2016-10-10 9 views
0

xml의 텍스트 및 텍스트 모양 변경 가능성이있는 2 개의 TextViews로 사용자 정의보기를 만들고 싶습니다. 이보기에는 두 개의 state-normal 및 selected가 있어야합니다 (TextViews 스타일은 각 상태마다 달라야 함).두 개의 TextView가있는 사용자 정의보기

몇 가지 예가 필요합니다.

+0

사용자 정의'ViewGroup' 클래스가 필요합니다. – pskink

답변

8

사용자 지정보기는 매우 기본적이고 인터넷을 통해 예제가 있습니다. 2 개의 텍스트 뷰와 같은 단순한 경우, 일반적으로 LinearLayout을 확장하는 것이 가장 쉽습니다.

두 개의 텍스트보기가 나란히 배치 된 LinearLayout입니다.

고해상도/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> 

은 다음으로 우리는 그래서 우리는 사용자가 우리의 사용자 정의 레이아웃 속성을 추가 할 수 styleable 자원 블록을 정의합니다.

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

    </declare-styleable> 
</resources> 

다음 DoubleText 사용자 정의 뷰 클래스 파일

고해상도/값/attrs.xml이. 여기에서는 사용자 정의 속성을 추출하고 각 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> 

쉬운 peasy.

+0

거기에 작은 주석이 있습니다 : 대답은 훌륭하지만 사용자 정의보기 xml의 작은 변화가 그리워요. 둘 다'TextView'가 필요합니다 android : layout_weight = "1" ' . 그렇지 않다면 첫 번째'TextView' 만 화면에 나타납니다. –

관련 문제