2016-10-10 38 views
0

我想用2個TextViews創建一個自定義視圖,可能會改變xml中的文本和文本外觀。這個視圖應該有兩個狀態 - 正常和選擇(TextViews樣式應該對每個狀態都不相同)。使用兩個TextViews的自定義視圖

我需要一些例子。

+0

所以你需要一個自定義的'ViewGroup'類 – pskink

回答

8

自定義視圖是非常基本的,並有互聯網上的例子。對於像兩個文本視圖一樣簡單的事情,擴展LinearLayout通常是最容易的。

這是帶兩個文本視圖的LinearLayout並排排列。

RES/double_text.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/left_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:maxLines="1"/> 
    <TextView 
     android:id="@+id/right_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:maxLines="1"/> 
</LinearLayout> 

接下來我們定義設置樣式的資源塊,所以我們可以添加自定義屬性到我們的自定義佈局。

RES /值/ attrs.xml

<resources> 
    <declare-styleable name="DoubleText"> 
     <attr name="leftText" format="string" /> 
     <attr name="rightText" format="string" /> 
     <attr name="android:ems" /> 

    </declare-styleable> 
</resources> 

接着爲DoubleText自定義視圖中的類文件。在這裏,我們拉出自定義屬性並設置每個TextView。

DoubleTextView.java

public class DoubleTextView extends LinearLayout { 
    LinearLayout layout = null; 
    TextView leftTextView = null; 
    TextView rightTextView = null; 
    Context mContext = null; 

    public DoubleTextView(Context context) { 

     super(context); 
     mContext = context; 
    } 

    public DoubleTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     mContext = context; 

     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DoubleText); 

     String leftText = a.getString(R.styleable.DoubleText_leftText); 
     String rightText = a.getString(R.styleable.DoubleText_rightText); 

     leftText = leftText == null ? "" : leftText; 
     rightText = rightText == null ? "" : rightText; 

     String service = Context.LAYOUT_INFLATER_SERVICE; 
     LayoutInflater li = (LayoutInflater) getContext().getSystemService(service); 

     layout = (LinearLayout) li.inflate(R.layout.double_text, this, true); 

     leftTextView = (TextView) layout.findViewById(R.id.left_text); 
     rightTextView = (TextView) layout.findViewById(R.id.right_text); 

     leftTextView.setText(leftText); 
     rightTextView.setText(rightText); 

     a.recycle(); 
    } 

    public DoubleTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     mContext = context; 
    } 

    @SuppressWarnings("unused") 
    public void setLeftText(String text) { 
     leftTextView.setText(text); 
    } 

    @SuppressWarnings("unused") 
    public void setRightText(String text) { 
     rightTextView.setText(text); 
    } 

    @SuppressWarnings("unused") 
    public String getLeftText() { 
     return leftTextView.getText().toString(); 
    } 

    @SuppressWarnings("unused") 
    public String getRightText() { 
     return rightTextView.getText().toString(); 
    } 

} 

最後,使用自定義類是在佈局文件中聲明一樣簡單。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/activity_main" 
    tools:context=".MainActivity"> 

    <TextView 
     android:id="@+id/main_text" 
     android:text="Hello World!" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/custom"/> 

    <example.com.test.DoubleTextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:leftText="Text Left" 
     app:rightText="Text Right" 
     android:layout_below="@+id/main_text"/> 
</RelativeLayout> 

容易心慌。

+0

那裏有一個小評論:答案很好,但是它錯過了自定義視圖的一個小小的改變xml:'TextView'都需要'android:layout_weight =「1」 ' 。如果不是,只是第一個'TextView'出現在屏幕上。 –

相關問題