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对象时,循环约束控制柄将消失。习惯于点击并拖动它们到合适的位置:)