【发布时间】:2013-09-30 09:52:42
【问题描述】:
我正在我的 Android 应用程序中制作一个设置屏幕。这个列表当然有一组预定义的选项(通知/颜色/注销/等)。所以我决定在 xml 中静态创建列表。因为如果列表的内容比我在ScrollView 中嵌套的LinearLayout 的屏幕内容多,列表应该同样好显示(不能使用ListView,因为我想静态定义其中的项目)。
现在使用以下代码可以正常工作:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black" >
<TextView
android:layout_width="fill_parent"
android:layout_height="80dp"
android:paddingTop="30dp"
android:text="@string/account_notifications"
android:background="@color/white"
android:layout_marginBottom="1dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="80dp"
android:text="@string/colors"
android:background="@color/white"
android:layout_marginBottom="1dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="80dp"
android:text="@string/log_out"
android:background="@color/white"
android:layout_marginBottom="1dp" />
</LinearLayout>
</ScrollView>
如您所见,我将LinearLayout 的背景设置为黑色,将项目设置为白色,marginBottom 的1dp,因此列表项之间有一个分隔符。我现在希望注销TextView 始终位于屏幕底部。万一有更多的项目超出屏幕可以容纳,注销应该简单地位于列表的末尾。
为了让注销到屏幕底部,我在this SO question 中找到了一些解决方案。我首先像这样使用第一个建议:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black" >
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:text="@string/account_notifications"
android:background="@color/white"
android:layout_marginBottom="1dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:text="@string/colors"
android:background="@color/white"
android:layout_marginBottom="1dp" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:text="@string/log_out"
android:background="@color/white"
android:layout_marginBottom="1dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</ScrollView>
这令人惊讶地使注销TextView 完全消失。所以我尝试了第二个建议:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black" >
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:layout_weight="1"
android:text="@string/account_notifications"
android:background="@color/white"
android:layout_marginBottom="1dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:layout_weight="1"
android:text="@string/colors"
android:background="@color/white"
android:layout_marginBottom="1dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:text="@string/log_out"
android:background="@color/white"
android:layout_marginBottom="1dp" />
</LinearLayout>
</ScrollView>
但不幸的是,这也没有任何作用。不知道为什么不。
所以我的问题是,有没有人知道我怎样才能让我的注销到屏幕底部,或者如果列表太长,到列表底部。
欢迎所有提示!
【问题讨论】:
-
您想在列表视图中拥有注销按钮吗?或者你想要列表视图,然后列表视图之后的注销按钮?
-
如何在第二个代码的LinearLayout中使用“layout_above”属性。前任。 android:layout_above="@+id/log_out_textview"
标签: android xml android-layout android-linearlayout textview