【问题标题】:Android Layout dynamic widthAndroid Layout 动态宽度
【发布时间】:2016-10-07 11:25:17
【问题描述】:

是否可以动态创建宽度为 50% 的布局,因此第三个布局低于第一个布局?我试过android:layout_weight=".5",但它不起作用。

编辑:目前还没有正确的答案

【问题讨论】:

  • "so 3rd one to be on the bottom of first first" 是什么意思?
  • 第一个下

标签: android layout width


【解决方案1】:

您可以使用 LinearLayout 进行以下操作:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:gravity="left"
    android:orientation="horizontal">
    <TextView
        android:layout_weight="1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:text="Takes up half the width" />
</LinearLayout>

通过在父项上设置 weightSum,您是说子项的权重应该等于该数量。通过将独生子女的体重设置为一半,它将占用一半的空间。确保将子项的宽度设置为 0,以便它知道使用权重值来计算其相对于其父项的空间。

【讨论】:

  • 不工作。第三个布局不在第一个下
  • 您的答案与我所说的在描述中不起作用的答案相同,但是您使用 2 和 1 而不是 weightSum 1 和 weight .5
【解决方案2】:

你可以使用Percent Support Library:

添加库

编译'com.android.support:percent:23.3.0'

Example:

 <android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentFrameLayout>

【讨论】:

  • 我需要在 LinearLayout 上使用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多