【问题标题】:How can create a Android Layout with two layout with ListView如何使用 ListView 创建具有两个布局的 Android 布局
【发布时间】:2015-06-10 09:32:09
【问题描述】:

我无法正确设置 Android 布局!

在我的代码中,ListView 填满了屏幕。 如果我用数字更改 ListView 上的 layout_height,则不适用于所有分辨率!

我使用这个代码:

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background">

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

        <ListView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/mainListView"
            >
        </ListView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/SecondLinearLayout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TableLayout
            android:layout_marginTop="10dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">
            <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:onClick="testButton"
            android:clickable="true"
            android:background="@drawable/search_button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:onClick="exitButton"
            android:layout_marginLeft="60dp"
            android:background="@drawable/exit_button" />
            </TableRow>
        </TableLayout>
    </LinearLayout>

</RelativeLayout>

我希望是这样的:

  • 滚动列表
  • 两个按钮(在底部)

我哪里错了?

谢谢

【问题讨论】:

    标签: android android-layout listview button


    【解决方案1】:

    你想这样做:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    android:weightSum="1" >
    
    <LinearLayout
        android:id="@+id/FirstLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.9"
        android:orientation="vertical" >
    
        <ListView
            android:id="@+id/mainListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/SecondLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:gravity="center"
        android:orientation="horizontal" >
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:clickable="true"
            android:onClick="testButton" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="60dp"
            android:background="#000000"
            android:onClick="exitButton" />
    </LinearLayout>
    

    【讨论】:

    • 完美!非常感谢!
    【解决方案2】:

    在第一个 LinearLayout 中使用 layout_above

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/FirstLinearLayout"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@id/SecondLinearLayout"
            >
    

    【讨论】:

      【解决方案3】:
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <LinearLayout
          android:id="@+id/linearlayout"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_above="@+id/linearlayout_second"
          android:layout_marginBottom="10dp"
          android:layout_weight="0.9"
          android:orientation="vertical">
          <ListView
              android:id="@+id/listview"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
          </ListView>
      </LinearLayout>
      
        <LinearLayout
          android:id="@+id/linearlayout_second"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_weight="0.1"
          android:gravity="center|bottom"
          android:orientation="horizontal">
      
          <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:clickable="true"
              android:onClick="testButton"
              android:text="button 1" />
      
          <Button
              android:id="@+id/button2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="60dp"
              android:text="button 2" />
      </LinearLayout>
      </RelativeLayout>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 2014-06-26
      • 2023-03-25
      • 1970-01-01
      • 2013-12-25
      相关资源
      最近更新 更多