1

我试图让 ListView 即使溢出也保持相同的大小。但是,一旦 ListView 溢出,它会将下面的按钮推离屏幕。如何防止它推动它下方的其他元素?

从上面的图片可以看出,第二张图显示一旦 ListView 溢出,按钮就会被推离屏幕。

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/splash_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout 
    android:background="@color/splash_bg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:paddingBottom="10dp"
        android:textColor="@color/splash_text"
        android:text="@string/sub" />

    <EditText 
        android:id="@+id/grp_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/field1"
        android:lines="1" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/members"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textSize="15sp"
            android:paddingTop="5dp"
            android:textColor="@color/splash_text"
            android:text="@string/members" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/splash_bg"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/name"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="@string/field2" />

            <Button
                android:id="@+id/add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/add" />

        </LinearLayout>

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:background="@color/splash_bg" />

    </LinearLayout>

</LinearLayout>

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:gravity="bottom"
     android:paddingLeft="5dp"
     android:paddingRight="5dp"
     android:paddingBottom="5dp"
     android:paddingTop="10dp" >

    <Button 
        android:id="@+id/save_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text="Save" />

     <Button 
        android:id="@+id/cancel_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />

</LinearLayout>

4

3 回答 3

8

变化

  <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:background="@color/splash_bg" />

  <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:background="@color/splash_bg" />
于 2013-06-11T15:51:11.393 回答
2

您可以为此使用相对布局。根据您的需要修改以下内容

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginRight="11dp"
        android:layout_marginTop="11dp"
        android:text="Create New Group" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="23dp"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="11dp"
        android:layout_marginTop="18dp"
        android:text="Members" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="31dp"
        android:ems="10" />

    <ListView
        android:id="@android:id/list"
        android:layout_above="@+id/button1"
        android:layout_below="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
         />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="13dp"
        android:text="ADD" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button3"
        android:layout_below="@android:id/list"
        android:text="Cancel" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/textView1"
        android:text="Save" />

</RelativeLayout>

在此处输入图像描述

于 2013-06-11T16:20:46.887 回答
-2

我添加了这一行:android:layout_weight="1"到存在 EditText 的 LinearLayout (我的问题来源)并且问题消失了!
看看我评论的代码<!-- HERE!!! -->


大声笑我知道我回答这个问题很晚了,对不起,但是对于仍在挣扎的人!

在此处输入图像描述


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linearLayout1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="ID : "
            android:layout_margin="@dimen/EdgeMargins"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText"
            android:layout_margin="@dimen/EdgeMargins"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearLayout2"
        android:layout_weight="1"> <!-- HERE!!! -->

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Any Notes :"
            android:id="@+id/textView2"
            android:layout_margin="@dimen/EdgeMargins"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_margin="@dimen/EdgeMargins" />

    </LinearLayout>

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Done"
        android:id="@+id/button"
        android:layout_margin="@dimen/EdgeMargins" />

</LinearLayout>


我对图片中突出显示的 EditText 有疑问,它会随着输入的文本展开并按下 DONE 按钮!
我知道我的解决方案可能看起来很傻,但我花了一整天的时间来解决这个问题!

希望对您有所帮助^_^ GL

于 2016-03-24T22:11:19.907 回答