【问题标题】:android fit panel to bottomandroid将面板调整到底部
【发布时间】:2011-12-27 07:14:58
【问题描述】:

我的表单上有两个按钮,保存和取消。当我进入表格时,键盘会自动显示。 (顺便说一句。Form 有一个 EditText)。我真的不知道如何将按钮放在底部。现在按钮放置在 EditText 下方,我想将它们放在键盘上方。

对于想要查看代码的人:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollView01" android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" style="@style/Form">

        <LinearLayout android:id="@+id/linearLayout1" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:layout_margin="7dp"
        android:orientation="vertical">

            <EditText 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:id="@+id/etNewListNameForm" 
                android:hint="list name"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"></EditText>

        </LinearLayout>

        <LinearLayout style="@style/BottomPanel"
            android:id="@+id/linearLayout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <Button
                android:id="@+id/btnSaveCardList" 
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginTop="5dp"
                android:text="Save"></Button>
            <Button
                android:id="@+id/btnCancelCardLst" 
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:text="Cancel"></Button>

        </LinearLayout>

    </LinearLayout>
</ScrollView>

最好的问候

图片:

【问题讨论】:

    标签: android vertical-alignment


    【解决方案1】:

    您有一个选择是使用相对布局而不是线性布局并使用 alignParentBottom 属性,如下所示:

        <RelativeLayout android:id="@+id/linearLayout1" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:layout_margin="7dp"
        android:orientation="vertical">
    
            <EditText 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:id="@+id/etNewListNameForm" 
                android:hint="list name"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"></EditText>
    
        </LinearLayout>
    
        <LinearLayout style="@style/BottomPanel"
            android:id="@+id/linearLayout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true">
            .....
    

    编辑:同样值得注意的是,您应该非常认真地考虑为您的视图使用更具描述性的名称。您为按钮和 EditTexts 使用了描述性名称,但在布局视图中使用了 linearLayout1 和 linearLayout2。考虑将它们命名为 mainLayout 和 bottomPannelLayout 之类的名称,这样在您尝试弄清楚如何设置它时会更容易。

    【讨论】:

    • 是的,我知道,但现在我只练习组件放置。我对名称有疑问,模式应该是什么?像这样的东西:FormName.Type.Name?最佳做法是什么?
    【解决方案2】:

    在包含 EditText 的 LinearLayout 中,将 android:layout_height 更改为 0dp 并将 android:layout_weight 设置为 1。这样,在按钮面板占据其份额后,它将占用父级中的其余空间。

    ...
        <LinearLayout android:id="@+id/linearLayout1" 
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_margin="7dp"
            android:orientation="vertical">
    
            <EditText 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:id="@+id/etNewListNameForm" 
                android:hint="list name"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"></EditText>
    
        </LinearLayout>
    ...
    

    【讨论】:

      【解决方案3】:
      1. 将 ScrollView 包装在 RelativeLayout 中,
      2. 将带有按钮的 LinearLayout 移动为该 RelativeLayout 的第二个子项,
      3. 并将该 LinearLayout 的 android:layout_alignParentBottom 设置为 true。

      【讨论】:

        【解决方案4】:

        删除不必要的视图并使用 relativeLayuot

        <?xml version="1.0" encoding="utf-8"?>
        
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:background="#ff0022">
        
        
            <LinearLayout android:id="@+id/linearLayout2"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_alignParentBottom="true">
                <Button android:layout_marginTop="5dp" android:text="Save"
                    android:layout_width="fill_parent" android:layout_height="wrap_content"
                    android:layout_weight="1" android:id="@+id/btnSaveCardList"></Button>
                <Button android:layout_marginTop="5dp" android:text="Cancel"
                    android:layout_width="fill_parent" android:layout_height="wrap_content"
                    android:layout_weight="1" android:id="@+id/btnCancelCardLst"></Button>
        
            </LinearLayout>
            <EditText android:layout_height="wrap_content"
                android:layout_margin="7dp" android:hint="list name"
                android:layout_width="fill_parent" android:id="@+id/etNewListNameForm"
                android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp">
                <requestFocus></requestFocus>
            </EditText>
        </RelativeLayout>
        

        【讨论】:

        猜你喜欢
        • 2019-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-28
        • 1970-01-01
        • 2016-09-04
        相关资源
        最近更新 更多