高度一半填充父布局(xml)。

11

我该如何在xml中将LinearLayout的高度设置为半个fill_parent

<LinearLayout android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:weightSum="1">
      <ImageView android:layout_height="fill_parent"
                 android:layout_width="0dp"
                 android:layout_weight="0.5"/>
</LinearLayout>

我希望能够用类似的方式处理高度。


不要使用fill_parent,而是指定所需的dp值。例如100dp或任何您想要的值。 - Raghunandan
你想让你的linearLayout成为一个正方形吗? - Blackbelt
我只想要屏幕的一半有一个LinearLayout,另一半有另一个LinearLayout。 - lexell
1个回答

35

我只想让屏幕的一半显示一个LinearLayout,另一半显示另一个LinearLayout

试试这个:

<?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:weightSum="2"
    android:orientation="vertical" >

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#192832"></LinearLayout>

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#193222"></LinearLayout>


</LinearLayout>

两个线性布局,每个占据屏幕的一半。


1
您还可以使用layout_height="0dp"来设置固定高度。 - Bibaswann Bandyopadhyay
4
注意,fill_parent 已经被弃用。API 8 或更高版本,请使用 match_parent。(文档 - ki9

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