Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 具有两个文本视图的自定义视图_Android_Custom View_Viewgroup_Android Viewgroup - Fatal编程技术网

Android 具有两个文本视图的自定义视图

Android 具有两个文本视图的自定义视图,android,custom-view,viewgroup,android-viewgroup,Android,Custom View,Viewgroup,Android Viewgroup,我想创建一个带有2个文本视图的自定义视图,可以从xml更改文本和文本外观。此视图应具有两种状态-正常和选定(TextViews样式对于每个状态应不同) 我需要一些例子。自定义视图非常基本,互联网上到处都有例子。对于两个文本视图这样的简单视图,通常最容易扩展LinearLayout 这是两个文本视图并排排列的线性布局 res/double\u text.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

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


我需要一些例子。

自定义视图非常基本,互联网上到处都有例子。对于两个文本视图这样的简单视图,通常最容易扩展LinearLayout

这是两个文本视图并排排列的线性布局

res/double\u 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>
<resources>
    <declare-styleable name="DoubleText">
        <attr name="leftText" format="string" />
        <attr name="rightText" format="string" />
        <attr name="android:ems" />

    </declare-styleable>
</resources>
最后,使用自定义类就像在布局文件中声明它一样简单

<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
need
android:layout\u weight=“1”
。如果没有,屏幕上只会出现第一个
TextView
<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>