【问题标题】:How to make layout to be 80% of the width of the screen?如何使布局为屏幕宽度的 80%?
【发布时间】:2013-07-23 18:27:02
【问题描述】:

无论屏幕尺寸或 DPI 是多少,我怎样才能使某些布局成为屏幕宽度的 80% 或 90%?我尝试使用 " 但我得到了 80% 的高度。

编辑:

    <?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"
    android:background="@drawable/background" >

    <Button
        android:id="@+id/bNivoi1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="80dp"
        android:background="@drawable/button_nivoi_back" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvOpis15Nivoa"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="15sp"
        android:layout_weight="0.8"
        android:text="text" />
    </LinearLayout>

</LinearLayout>

我只需要 textview 占屏幕的 80%,而不是活动的其余部分。

【问题讨论】:

  • 按照@ianhanniballake 的回答,将现有布局包装在水平方向的LinearLayout 中(您无需明确指定,默认为水平),然后使用android:layout_weight="0.8"
  • 不知何故它不起作用。让我编辑我的帖子并粘贴我的 XML。

标签: java android xml


【解决方案1】:

android:layout_weight 是基于 android:orientationLinearLayout - 如果您希望布局占据 80% 的宽度,您需要 LinearLayoutandroid:orientation="horizontal"

<LinearLayout
    android:id="@+id/your_outer_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" > <!-- note that vertical is the default -->

    <!-- Other elements here -->

    <LinearLayout
        android:id="@+id/eighty_percent_layout_holder
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">
        <View
            android:id="@+id/your_eighty_percent_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.8" />
     </LinearLayout>

    <!-- Other elements here -->
</LinearLayout>

【讨论】:

  • @marjanbaz - 如果您仍有问题,我已经添加了一些示例 XML。
  • 我完全按照你的做法做了,但仍然一无所获。还是100%。我编辑了我的第一篇文章。
  • 感谢android:weightSum="1"
  • 细微差别改变了一切:“你需要一个带有 android:orientation="horizo​​ntal" 的 LinearLayout。谢谢,工作得很好!甚至可以与 CardView 一起使用(当父级是 LinearLayout 与 wieghtsum 水平时)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多