【问题标题】:Custom View with two TextViews带有两个 TextView 的自定义视图
【发布时间】:2016-10-10 14:50:40
【问题描述】:

我想创建一个带有 2 个 TextViews 的自定义视图,可以从 xml 更改文本和文本外观。这个视图应该有两种状态 - 正常和选中(每个状态的 TextViews 样式应该不同)。

我需要一些例子。

【问题讨论】:

  • 所以你需要一个自定义的ViewGroup

标签: android custom-view viewgroup android-viewgroup


【解决方案1】:

自定义视图非常基础,互联网上到处都有示例。对于像两个文本视图这样简单的东西,扩展 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/values/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>

简单易懂。

【讨论】:

  • 那里有一个小评论:答案很好,但它错过了自定义视图 xml 中的一个小变化:TextView 都需要 android:layout_weight="1" 。如果没有,屏幕上只会出现第一个TextView
猜你喜欢
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
  • 2015-12-29
相关资源
最近更新 更多