Layout for Android. Fluid layout and a fixed layout

爱⌒轻易说出口 提交于 2019-12-24 02:22:09

问题


I've to develop an Android application that needs to show data using a similar layout. I need two parts of screen:

  • A layout with a fluid height that vary on resolution of device
  • Second layout that show content in a fixed size

How to do this? I've used relativelayout and linearlayout but I haven't found a solution. Could you help me?


回答1:


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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/fixedlayout"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:id="@+id/fluidlayout" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:orientation="vertical"
        android:id="@+id/fixedlayout" >
    </LinearLayout>

</RelativeLayout>

More efficient solution using LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp" >
    </LinearLayout>

</LinearLayout>



回答2:


For a LinearLayout set top layout to android:layout_weight="1" (and height to 0dp) and bottom layout_height to either a fixed value or android:layout_height="wrap_content"

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

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FF0000" />

    <View
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#00FF00" />

</LinearLayout>


来源:https://stackoverflow.com/questions/12013661/layout-for-android-fluid-layout-and-a-fixed-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!