【发布时间】:2017-11-15 21:50:37
【问题描述】:
我正在为我正在构建的设置屏幕创建一些自定义首选项。布局相当简单,两个文本视图和一个图像视图。我希望 textviews 包装和偏好视图根据里面的内容增长。暂时忽略约束我希望使用约束布局设置此设置,但最初我认为我可能在那里做错了什么,所以在我进行故障排除时使用了基本的相对布局。
我已经设置了几个不同的基本布局,但似乎有一个不能轻易覆盖的最大高度?
<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="wrap_content">
<TextView
android:id="@+id/motivatingTitleTextView"
android:layout_width="wrap_content"
app:layout_constraintWidth_default="wrap"
android:layout_height="wrap_content"
android:text="@string/add_motivating_photo_title"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/motivatingMessageTextView"
android:layout_below="@id/motivatingTitleTextView"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
app:layout_constraintWidth_default="wrap"
android:layout_height="wrap_content"
android:text="@string/add_motivating_photo_message"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/motivatingTitleTextView"
app:layout_constraintBottom_toBottomOf="parent"/>
</RelativeLayout>
这里是设置xml
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.PreferenceCategory
android:title="Notifications and Access">
<android.support.v7.preference.SwitchPreferenceCompat
android:key="allowPushNotifications"
android:title="Notifications"
android:defaultValue="true" />
</android.support.v7.preference.PreferenceCategory>
<android.support.v7.preference.PreferenceCategory
android:title="Personalization">
<com.company.Settings.AddMotivatingPhotoPreference/>
</android.support.v7.preference.PreferenceCategory>
最后是偏好片段 公共类 SettingsFragment 扩展 PreferenceFragmentCompat {
public SettingsFragment()
{
// Required empty public constructor
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
{
addPreferencesFromResource(R.xml.settings);
}
}
编辑:
所以我发现如果我在默认的 Preference 对象中使用布局标签,它的布局是正确的。我可以完成这项工作,但我希望能够创建自己的自定义类,以便我可以将其他逻辑分离到它们中。有没有人这样做过或有一个例子?我觉得我一定只是错过了一些简单的东西......
<android.support.v7.preference.Preference
android:layout="@layout/preference_add_motivating_photo"/>
【问题讨论】:
标签: android android-layout preferencefragment