如何在列表视图下的屏幕底部获取TextView?

时间:2013-09-30 09:52:43

标签: android xml android-layout android-linearlayout textview

我在我的Android应用程序中进行设置屏幕。该列表当然具有预定义的选项集(通知/颜色/注销/等)。所以我决定在xml中静态创建列表。因为如果列表的内容多于屏幕,则列表应该同样好显示我将LinearLayout嵌套在ScrollView内(无法使用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的背景设置为黑色,将项目设置为白色,marginBottom1dp,以便列表项之间有分隔符。我现在希望注销TextView始终位于屏幕底部。如果项目的数量超出了屏幕所能容纳的数量,则Logout应该只位于列表的末尾。

要退出屏幕底部,我在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>

但不幸的是,这也没有做任何事情。不知道为什么不。

所以我的问题是,是否有人知道如何将我的注销退出到屏幕底部,或者如果列表是长屏幕,则列在列表的底部。

欢迎所有提示!

4 个答案:

答案 0 :(得分:0)

Button btnLogout = new Button(this);
    btnLogout.setText("logout");

    // Adding logout button to lisview at bottom
    lv.addFooterView(btnLogout);

这里lv是listview对象。您可以使用textview或任何其他视图执行相同的操作。

答案 1 :(得分:0)

您可以添加列表视图的页脚视图,它将显示列表视图的底部。

    private View footerView;

    footerView = ((LayoutInflater) getActivity().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.footer_layout, null, false);

    TextView textview = (TextView) footerView
                    .findViewById(R.id.textview);

listview.addFooterView(footerView);

取一个布局名称footer_layout。在这一个布局中采取一个Textview。之后你可以使用上面的代码。 对你有帮助。

答案 2 :(得分:0)

// try this 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="color/black"
              android:orientation="vertical" >

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

            <TextView
                    android:layout_width="match_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="match_parent"
                    android:layout_height="80dp"
                    android:text="@string/colors"
                    android:background="@color/white"
                    android:layout_marginBottom="1dp" />
        </LinearLayout>
    </ScrollView>

    <TextView
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:text="@string/log_out"
            android:background="@color/white"
            android:layout_marginBottom="1dp" />
</LinearLayout>

答案 3 :(得分:0)

如果您要修复屏幕底部的退出TextView,可以试试这个:

    <ScrollView
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="color/black"
                  android:layout_gravity="center_vertical" >

          <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" >
            <LinearLayout
                    android:layout_alignParentTop="true"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                <TextView
                        android:layout_width="match_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="match_parent"
                        android:layout_height="80dp"
                        android:text="@string/colors"
                        android:background="@color/white"
                        android:layout_marginBottom="1dp" />
            </LinearLayout>
            <TextView
                android:id="@+id/txtLogout"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:text="@string/log_out"
                android:background="@color/white"
                android:layout_marginBottom="1dp"
                android:layout_alignParentBottom="true" />
       </RelativeLayout>
   </ScrollView>
相关问题