【问题标题】:Android: How to set layout height automaticAndroid:如何自动设置布局高度
【发布时间】:2016-04-25 19:58:52
【问题描述】:

我想像这张图一样自动设置布局高度:

因为如果我在fill_parentmatch_parent 中设置布局高度,它看起来像这样:

我想要fill_parent 之类的东西,但在中间。

抱歉英语不好,希望你能理解。

【问题讨论】:

  • 你为什么不在那个布局中使用权重?
  • 设置“包装内容”@Julian Pera
  • 你应该为父级使用RelativeLayout。
  • @Julián Pera 你能支持我的回答吗?
  • @Raghavendra 完成.. 谢谢!

标签: android layout height


【解决方案1】:

使用布局的布局权重属性

【讨论】:

  • 这种情况下怎么用?
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/aa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

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

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bb"
        android:layout_below="@+id/aa"></LinearLayout>

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

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

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

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

</RelativeLayout>

【讨论】:

    【解决方案3】:

    你可以使用

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
    

    根据您的内容自动设置布局

    【讨论】:

      【解决方案4】:

      让我给出一个疯狂的答案:)

      首先确保设置约束,然后像这样设置高度android:layout_height="zeltrax",然后运行应用程序并猜测什么构建输出会给你一个错误,其中包含适合该屏幕的精确 dp 数。

      像这样:AAPT: error: 'zeltrax' is incompatible with attribute layout_height (attr) dimension|enum [fill_parent=4294967295, match_parent=4294967295, wrap_content=4294967294]

      你必须在高度上使用wrap_content=4294967294,比如android:layout_height="4294967294dp",然后你就可以开始了。

      稍后谢谢我:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-19
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        • 2016-01-23
        • 1970-01-01
        • 2012-10-29
        相关资源
        最近更新 更多