使用带有ConstraintLayout的selectableItemBackground

时间:2017-05-23 14:10:21

标签: android textview android-constraintlayout

我正在尝试使用android:background="?android:attr/selectableItemBackground",但我不确定如何将其添加到ConstraintLayout中,我已将背景颜色应用于TextView。这就是我所拥有的:

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for app main screen -->
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/tan_background"
    tools:context="com.example.android.miwok.MainActivity">

    <TextView
        android:id="@+id/numbers"
        style="@style/CategoryStyle"
        android:background="@color/category_numbers"
        android:text="@string/category_numbers"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/family"
        style="@style/CategoryStyle"
        android:background="@color/category_family"
        android:text="@string/category_family"
        app:layout_constraintTop_toBottomOf="@+id/numbers" />

</android.support.constraint.ConstraintLayout>

使用LinearLayout,我知道我可以使用FrameLayout并将TextView嵌套在我可以将android:background="?android:attr/selectableItemBackground"android:background="@color/category_numbers"应用于{{1}的位置}}。然后可以对布局中的每个FrameLayout重复此操作。但是,当我为TextView尝试此操作时,它无法渲染。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

好的,所以我想出了问题。 LinearLayoutFrameLayoutandroid:orientation="vertical"只会在LinearLayout开始标记中使用ConstraintLayout时堆叠。使用FrameLayout时,我需要为每个LinearLayout的左侧,右侧和顶部添加约束,以便它们与LinearLayout堆叠相同。

如果有人对这里感兴趣的是<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/tan_background" android:orientation="vertical" tools:context="com.example.android.miwok.MainActivity"> <!-- Numbers category --> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/category_numbers"> <TextView android:id="@+id/numbers" style="@style/CategoryStyle" android:background="?android:attr/selectableItemBackground" android:text="@string/category_numbers" /> </FrameLayout> <!-- Family category --> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/category_family"> <TextView android:id="@+id/family" style="@style/CategoryStyle" android:background="?android:attr/selectableItemBackground" android:text="@string/category_family" /> </FrameLayout> </LinearLayout> 的XML:

ConstraintLayout

以下是<?xml version="1.0" encoding="utf-8"?> <!-- Layout for app main screen --> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@color/tan_background" tools:context="com.example.android.miwok.MainActivity"> <FrameLayout android:layout_width="0dp" android:layout_height="wrap_content" android:background="@color/category_numbers" android:id="@+id/frameNumbers" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent"> <TextView android:id="@+id/numbers" style="@style/CategoryStyle" android:text="@string/category_numbers" /> </FrameLayout> <!-- Family category --> <FrameLayout android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/frameFamily" android:background="@color/category_family" app:layout_constraintTop_toBottomOf="@+id/frameNumbers" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent"> <TextView android:id="@+id/family" style="@style/CategoryStyle" android:text="@string/category_family" /> </FrameLayout> </android.support.constraint.ConstraintLayout> 的XML:

> projet
........projet.pro
........>Sources
..............main.py
........>Ressources
................>qml.qrc
.....................>content
..........................Etale.qml
......................main.qml
......................Page.qml
......................Page1.qml

出于某种原因,只要我使用FrameLayout,圆形约束句柄就会消失。