Android ListView项目的位置
问题描述:
我想要创建ListView,其中的项目在屏幕截图上的位置就像。因此,文本将在左侧并切换到右侧。我知道这应该由TableLayout完成,但我无法弄清楚。感谢所有帮助。 Android ListView项目的位置
答
您可以使用SwitchPreference
轻松实现此目的。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="true"
android:key="key_data_saver"
android:title="Data Saver"
android:summary="Enable click-to-download for images and videos when using mobile data" />
</PreferenceScreen>
这里是一个有用的教程:Android Implementing Preferences Settings Screen
OR,
如果你想不SwicthPreference
创建自己的,然后使用RecyclerView
与custom layout
每个row
项目。
这是你预期的行项目布局XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<Switch
android:id="@+id/switchRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:checked="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toLeftOf="@id/switchRight"
android:layout_marginRight="8dp">
<TextView
android:id="@+id/textTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18sp"
android:text="Data Saver"/>
<TextView
android:id="@+id/textDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textColor="#727272"
android:textSize="16sp"
android:text="Enable click-to-download for images and videos when using mobile data"/>
</LinearLayout>
</RelativeLayout>
OUTPUT:
希望这将有助于〜
答
来实现这一点的最好办法带有PreferenceFragment。
您可以使用所有设置创建一个XML文件。 (比方说, “settings.xml的” 文件夹 “XML” 里,你可以创建内部 “资源”)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="@string/settings_category_app">
<SwitchPreference
android:defaultValue="false"
android:key="@string/settings_notification_switch"
android:summary="Enable Click to download images and videos..."
android:title="Data Saver" />
</PreferenceCategory>
更新它是一个静态类,因为我在主要活动内创建了
之后,您可以将此资源添加到您的SettingsFragment中:
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
然后你可以使用这个片段作为任何其他的。 (例如在抽屉里)
答
TableLayout不需要在你的情况下,因为你没有任何表。你必须使用的RelativeLayout代替:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="wrap_content"
android:width="match_parent"
>
<TextView
android:height="wrap_content"
android:width="wrap_content"
android:id="@+id/tvTitle"
android:layout_alignParentLeft="true"
android:text="Title"
/>
<TextView
android:height="wrap_content"
android:width="wrap_content"
android:id="@+id/tvSubTitle"
android:layout_below="@id/tvTitle"
android:text="Subtitle"
/>
<ToggleButton
android:height="wrap_content"
android:width="wrap_content"
android:id="@+id/tbSwitch"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="switch"
/>
/>
对于切换按钮的U可以使用这个库IOS造型:https://github.com/kyleduo/SwitchButton
http://www.journaldev.com/10416/android-listview-with-custom- adapter-example-tutorial https://*.com/questions/8166497/custom-adapter-for-list-view –