【问题标题】:View half of its parent's height and width查看其父级的一半高度和宽度
【发布时间】:2014-10-14 19:56:07
【问题描述】:

我如何使一个android视图的宽度和高度为父视图的一半?像这里加星标的部分:

+-------|-------+
|*******|       |
|*******|       |
|*******|       |
|*******|       |
|*******|       |
|*******|       |
|-------|-------|
|       |       |
|       |       |
|       |       |
|       |       |
|       |       |
|       |       |
+-------|-------+

编辑

任何阅读此问题的人,您最好使用 AppCompat GridLayout 并使用 column_weight 解释 here 因为嵌套权重很昂贵。

【问题讨论】:

  • 一种方法是沿中间部分绘制两条宽度为 0dp 的线,并将视图相对于这些线对齐。
  • 但我的问题不是对齐,而是它的大小。我可以使用 align_parent_top 或类似的东西来对齐它们

标签: android android-layout


【解决方案1】:

一些虚假的布局和布局权重必须做到这一点..

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

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/dummy"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:visibility="invisible" >
    </LinearLayout>
</LinearLayout>

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

    <LinearLayout
        android:id="@+id/dummy"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:visibility="invisible" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/dummy"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:visibility="invisible" >
    </LinearLayout>
</LinearLayout>

</LinearLayout>

【讨论】:

    【解决方案2】:

    有多种方法可以做到这一点 - 取决于上下文 - 但一种方法是使用 layout_weight

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多