首页 > 解决方案 > 文本在自定义布局中的涟漪效果之上以获得偏好

问题描述

我想为我的设置设置一个 SwitchPreference 的自定义布局。我可以成功地将布局更改为我想要的,但我有以下行为。当您点击某物时,此项目的文本位于 android 的涟漪效果之上。

偏爱

下面我们可以看到首选项的简单版本

<PreferenceCategory 
android:layout="settings_style_title"   
app:iconSpaceReserved="false">
    
    <SwitchPreference
    android:defaultValue="true"
    android:key="a_key"
    android:title="this text gets above the effect"
    android:layout="my_custom_layout" />

</PreferenceCategory>

和 my_custom_layout。

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" 
android:layout_height="40dp"
android:gravity="center_vertical">
    
    <TextView   
    android:id="@android:id/title"      
    android:layout_width="wrap_content"     
    android:layout_height="wrap_content"        
    android:layout_alignParentLeft="true"       
    android:layout_marginLeft="15dp" 
    android:fontFamily="sans-serif"

    android:minHeight="25dp"

    android:scrollbars="none"
    
android:singleLine="true"

    android:textColor="#000000"
 
    android:textSize="18sp"/>

   <Switch      
    android:id="@android:id/switch_widget"      
    android:layout_width="wrap_content"     
    android:layout_height="wrap_content"    
    android:layout_alignParentRight="true"      
    android:layout_marginRight="15dp"/>

</RelativeLayout>

我也尝试过使用android:widgetLayout,但这仅控制小部件所在的空间,我无法为textview.

然后我就试着直接给属性上了SwitchPreference,只用了widgetLayout还是不行。

我也尝试在里面设置标题my_custom_layout

我也尝试删除android:background但没有效果。

我认为这与布局有关,因为当我检查它们时,偏好没有my_custom_layout如下所示:

没有自定义布局

而带有它的那个看起来像这样:

带有自定义布局

还有什么是widget_frame?我似乎找不到任何包含它的地方,my_custom_layout所以我可以拥有相同的结构。有什么想法吗?

标签: androidtextviewandroid-preferences

解决方案


推荐阅读