51

我刚刚在 LinearLayout 中实现了一个 ListView,但我需要定义 LinearLayout 的高度(它必须是屏幕高度的 50%)。

<LinearLayout
    android:id="@+id/widget34"
    android:layout_width="300px"
    android:layout_height="235px"
    android:orientation="vertical"
    android:layout_below="@+id/tv_scanning_for"
    android:layout_centerHorizontal="true">

    <ListView
        android:id="@+id/lv_events"
        android:textSize="18sp"         
        android:cacheColorHint="#00000000"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_scanning_for"
        android:layout_centerHorizontal="true">
    </ListView>

</LinearLayout>

那可能吗?

我为按钮和 EditText 做了类似的事情,但似乎不适用于布局。

这是我的代码:

    //capture the size of the devices screen
    Display display = getWindowManager().getDefaultDisplay();
    double width = display.getWidth();

    //my EditText will be smaller than full screen (80%)        
    double doubleSize = (width/5)*4;
    int editTextSize = (int) doubleSize;

    //define the EditText 
    userName = (EditText) this.findViewById(R.id.userName);
    password = (EditText) this.findViewById(R.id.password);

    //set the size
    userName.setWidth(editTextSize);
    password.setWidth(editTextSize);
4

10 回答 10

96

设置它的layout_height="0dp"*,View在它下面添加一个空白(或空白ImageView或只是一个FrameLayout),alayout_height也等于0dp,并将两个视图都设置为layout_weight="1"

这将在填充屏幕时平均拉伸每个视图。由于两者具有相同的重量,因此每个将占据屏幕的 50%。

*请参阅 adamp 的评论,了解为什么会这样以及其他真正有用的花絮。

于 2011-07-22T18:08:44.577 回答
8

这在 xml 中很容易做到。将顶部容器设置为 LinearLayout 并根据需要设置方向属性。然后在那个地方里面有两个线性布局,它们在宽度和高度上都有“填充父级”。最后,将这两个线性布局的权重属性设置为 1。

于 2011-07-22T18:09:33.997 回答
6

这是我的 android:layout_height=50% 活动:

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

    <LinearLayout
        android:id="@+id/alipay_login"
        style="@style/loginType"
        android:background="#27b" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/taobao_login"
        style="@style/loginType"
        android:background="#ed6d00" >
    </LinearLayout>

</LinearLayout>

风格:

<style name="loginType">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_weight">0.5</item>
    <item name="android:orientation">vertical</item>
</style>
于 2013-02-07T10:38:09.160 回答
3

最好的方法是使用

layout_height="0dp" layout_weight="0.5"

例如

<WebView
    android:id="@+id/wvHelp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" />

<TextView
    android:id="@+id/txtTEMP"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:text="TextView" />

WebView,TextView 有 50% 的屏幕高度

于 2015-01-12T21:52:19.863 回答
2

为了确保视图的高度是屏幕的 50%,我们可以在一个 LinearLayout 中创建两个子 LinearLayout。每个子 LinearLayout 应具有0.5的“ android:layout_weight ”以覆盖屏幕的一半

父 LinearLAyout 应将“ android:orientation ”设置为垂直

.

.

这是供您参考的代码....此代码包含两个高度为屏幕一半的按钮

<LinearLayout 
android:orientation="vertical"
android:layout_height="match_parent">

<LinearLayout

    android:layout_weight="0.5"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:padding="10dp"
        android:layout_weight="0.5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="button1"
        android:id="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        />

    <Button
        android:padding="10dp"
        android:layout_weight="0.5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="button2"
        android:id="@+id/button2"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        />

    </LinearLayout>

<LinearLayout

    android:layout_weight="0.5"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    </LinearLayout>
   </LinearLayout>
于 2016-07-23T07:33:29.450 回答
1

这种对我有用。虽然 FAB 没有独立浮动,但现在它没有被推倒。

观察 LinearLayout 中给出的权重

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/sharedResourcesRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="4"
                />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="bottom|right"
                android:src="@android:drawable/ic_input_add"
                android:layout_weight="1"/>

        </LinearLayout>

希望这可以帮助 :)

于 2016-07-31T09:51:26.540 回答
0

你应该这样做:

<LinearLayout
    android:id="@+id/widget34"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_below="@+id/tv_scanning_for"
    android:layout_centerHorizontal="true">

    <ListView
        android:id="@+id/lv_events"
        android:textSize="18sp"         
        android:cacheColorHint="#00000000"
        android:layout_height="1"
        android:layout_width="fill_parent"
        android:layout_weight="0dp"
        android:layout_below="@+id/tv_scanning_for"
        android:layout_centerHorizontal="true"
        />

</LinearLayout>

也可以使用dp代替px或在 此处阅读。

于 2011-07-22T18:18:17.600 回答
0

如果您想将屏幕垂直分成两部分(top30% + bottom70%),这很容易

<LinearLayout
            android:id="@+id/LinearLayoutTop"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2">

     </LinearLayout>
     <LinearLayout
            android:id="@+id/LinearLayoutBottom"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">

     </LinearLayout>
于 2015-01-12T18:27:49.790 回答
0

要实现这一壮举,请使用weightSum={amount of weight to distribute}.

它定义了最大重量总和。如果未指定,则通过添加所有子项的 layout_weight 来计算总和。例如,这可以用来给单个孩子 50% 的总可用空间,方法是给它一个 layout_weight 为 0.5 并将 weightSum 设置为 1.0。另一个例子是设置 weightSum=2,如果两个孩子都设置了,layout_weight=1那么每个孩子都会获得 50% 的可用空间。

WeightSum 取决于父布局中子元素的数量。

于 2017-05-04T16:52:39.710 回答
-1

您可以android:weightSum="2"在父布局上与子布局结合使用android:layout_height="1"

           <LinearLayout
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                android:weightSum="2"
                >

                <ImageView
                    android:layout_height="1"
                    android:layout_width="wrap_content" />

            </LinearLayout>
于 2019-05-08T18:30:22.237 回答