安卓:布局高度为屏幕大小的50%。

53

我刚刚在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);

你尝试过在LinearLayout周围使用RelativeLayout并调整RelativeLayout的大小吗? - Kevin King
RelativeLayout的高度参数可以输入百分比吗? - MataMix
使用自定义的LayoutParams:http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html,结合RelativeLayout.setLayoutParams()方法:http://developer.android.com/reference/android/view/View.html#setLayoutParams%28android.view.ViewGroup.LayoutParams%29。将高度设置为display.getHeight()/2。 - Kevin King
10个回答

100

将其layout_height="0dp",在其下面添加一个空的View(或者空的ImageView或只是一个FrameLayout),并且这个Viewlayout_height也设为0dp,然后将两个视图都设置为layout_weight="1"

这样每个视图都会平均地拉伸以填满屏幕。由于两个视图的权重相同,因此它们各占屏幕的50%。

*参见adamp's评论,了解为什么这样做可行以及其他非常有用的提示。


24
使用权重时,请记住,权重会分配剩余空间 ,即在测量所有子视图后剩下的空间。如果您只想依靠权重,请将layout_height设置为“0dp”而不是“wrap_content”。您还可以在LinearLayout上显式设置weightSum,而不是添加空视图以填充总权重。 - adamp
@adamp 非常感谢您提供这个我不知道的优秀信息。谢谢。 - LeffelMania
宽度比例平衡怎么样? - Remario

8

这在xml中很容易实现。将您的顶级容器设置为LinearLayout,并根据需要设置方向属性。然后,在其中放置两个具有“fill parent”宽度和高度的线性布局。最后,将这两个线性布局的权重属性设置为1。


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>

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%。


2
为了确保视图的高度为屏幕的50%,我们可以在LinearLayout中创建两个子LinearLayout。每个子LinearLayout都应该有“android:layout_weight”设置为0.5,以覆盖屏幕的一半。
父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>

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>

希望这有所帮助 :)

0

如果你想将屏幕垂直分为两个部分(上30%+下70%),这很容易实现。

<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>

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或在这里阅读相关内容


2
交换您的身高和体重数值,这样就正确了。 ;) - adamp
是的,这样做在当前设备分辨率下是正确的。但如果测试在不同的分辨率上呢?那么根据新的显示分辨率,他将获得更多或更少的结果,而不是50%!对于静态高度/宽度来说,这有点棘手!学习如何为UI组件使用比例尺寸! - nenito
重量和身高仍然交换了值。 - Lay González

0
为了实现这一功能,请定义一个外部线性布局,其中包含一个weightSum={要分配的权重总量}
它定义了最大权重总和。如果未指定,则通过添加所有子项的layout_weight来计算总和。例如,可以通过将layout_weight设置为0.5并将weightSum设置为1.0,使单个子项使用可用空间的50%。另一个例子是设置weightSum=2,如果两个子项都设置layout_weight=1,则每个子项都会获得可用空间的50%。
WeightSum取决于父布局中子项的数量。

在这种情况下,children 可以是任何视图组(FrameLayout)或任何视图,例如 imageView 等。 - Remario

-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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接