Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.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 将selectableItemBackground与ConstraintLayout一起使用_Android_Textview_Android Constraintlayout - Fatal编程技术网

Android 将selectableItemBackground与ConstraintLayout一起使用

Android 将selectableItemBackground与ConstraintLayout一起使用,android,textview,android-constraintlayout,Android,Textview,Android Constraintlayout,我正在尝试使用android:background=“?android:attr/selectableitemsbackground”,但我不确定如何将其添加到ConstraintLayout中,我已经在文本视图中应用了背景色。这就是我所拥有的: <?xml version="1.0" encoding="utf-8"?> <!-- Layout for app main screen --> <android.support.constraint.Constrai

我正在尝试使用
android:background=“?android:attr/selectableitemsbackground”
,但我不确定如何将其添加到
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">

    <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/selectableitemsbackground”
android:background=“@color/category\u numbers”
应用到
FrameLayout
。然后可以对布局中的每个
TextView
重复此操作。但是,当我为
ConstraintLayout
尝试此操作时,它无法渲染


有什么想法吗?

好的,所以我解决了问题。在
LinearLayout
下,当使用
android:orientation=“vertical”
LinearLayout
开始标记中时,所有
FrameLayout
都会堆叠起来。当使用
ConstraintLayout
时,我需要为每个
FrameLayout
添加左、右和顶部的约束,以便它们的堆栈与
LinearLayout
相同

如果有人感兴趣,这里有
LinearLayout
的XML:

<?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 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>

出于某种原因,我一使用FrameLayout,圆形约束控制柄就会消失。

能否详细说明“无法渲染”?是什么阻止你做与你做过的相同的事情,用
ConstraintLayout
而不是
LinearLayout
?i、 e.将
TextView
放在
FrameLayout
中,将
FrameLayout
放在
ConstraintLayout
@Weizhi中,是的,就是这样。只是没有考虑到使用FrameLayout对象时,循环约束控制柄将消失。习惯于点击并拖动它们到合适的位置:)