【问题标题】:How to get a TextView at the bottom of the screen under a listview?如何在列表视图下的屏幕底部获取 TextView?
【发布时间】: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 的背景设置为黑色,将项目设置为白色,marginBottom1dp,因此列表项之间有一个分隔符。我现在希望注销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


【解决方案1】:
Button btnLogout = new Button(this);
    btnLogout.setText("logout");

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

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

【讨论】:

  • 问题是因为我想手动填充我的列表,所以我不能使用 listView。出于这个原因,我使用的是 LinearLayout,并且没有为 LinearLayout 类型使用 addFooterView 方法。还有其他想法吗?
【解决方案2】:
// 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】:

    你可以添加listview的footer view,它会显示在listview的底部。

        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。之后,您可以使用上面的代码。 可能对你有帮助。

    【讨论】:

    【解决方案4】:

    如果你想修复屏幕底部的注销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>
    

    【讨论】:

    • 感谢您的建议。问题是,如果屏幕容纳不下,就会开始重叠,这是不好的。如果超过屏幕可以容纳的内容,则该项目应简单地附加到列表的末尾。
    • 您是否希望将注销 textView 固定在屏幕底部。或者在你的滚动视图的底部?
    • 当列表没有填满整个屏幕(即:不够长)时,我希望将注销固定在屏幕底部。但是,当列表对于屏幕来说太长时(例如,在小屏幕的情况下)我希望将注销附加到列表的末尾。这甚至可能吗?
    • 您可以将 LinearLayout 替换为 RelativeLayout ,然后将 TextView Logout 放在 RelativeLayout 中,并将其与屏幕底部对齐。查看我的编辑
    猜你喜欢
    • 2013-03-15
    • 2019-05-02
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    相关资源
    最近更新 更多