【问题标题】:How to give layout weight in RelativeLayout?如何在RelativeLayout中赋予布局权重?
【发布时间】:2015-09-29 10:32:05
【问题描述】:

我使用LinearLayout 创建了一个拨号盘。这是代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:clickable="true"
                android:text="1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:clickable="true"
                android:text="2" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:text="3" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

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


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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:text="4" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:text="5" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:text="6" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">


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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:text="7" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:text="8" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:text="9" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:clickable="true"
                android:text="*" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:clickable="true"
                android:text="0" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="64dp"
                android:text="#" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

看起来像这样

现在我想在RelativeLayout 中使用相同的功能,但我发现layout_weightRelativeLayout 中不起作用。我也不想在RelativeLayout 中使用LinearLayout。 或者是否有任何替代方法可以像 layout_weightLinearLayout 中一样工作,但对于 RelativeLayout

【问题讨论】:

  • 你需要它做什么?为什么要使用RelativeLayout?为什么不把LinearLayout放到RelativeLayout中呢?
  • 我想用RelativeLayout创建一个拨号盘
  • 这并不能回答我的任何问题。
  • 有没有办法在RelativeLayout中分割屏幕,就像在LinearLayout中使用layout_weight一样?
  • RelativeLayout 中,您通常不需要权重,因为您可以指定一个视图以与另一个视图对齐。

标签: android android-layout layout android-linearlayout android-relativelayout


【解决方案1】:

您不能使用百分比来定义相对布局中视图的尺寸。最好的方法是使用 LinearLayout 和权重,或自定义布局。

更多信息你可以看看这个question

【讨论】:

    【解决方案2】:

    如果您想保持相对布局,您可以尝试通过代码更改位置。使用以下方法获取外部布局的高度和宽度:

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative);
    rl.getWidth();
    rl.getHeight();
    

    然后获取你想要均匀分布的布局参数:

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50); //here i created a new one with size 50,50
    

    现在您可以通过任何计算以您想要的方式设置params.leftMarginparams.topMargin

    params.leftMargin = rl.getWidth()/4 ;
    

    例如将新布局设置为屏幕宽度的 1/4

    【讨论】:

      【解决方案3】:

      RelativeLayout 不是一个好的选择,因为它无法像LinearLayout 那样为您提供均匀分配子代的方法。如果您想要嵌套 LinearLayouts 以外的其他内容,可以尝试使用 GridLayout不是 GridView)。

      【讨论】:

        【解决方案4】:

        您不能在RelativeLayout 中使用layout_weight。查看您的 XML,您当前的布局结构很差,这将导致性能不佳。我建议您使用GridLayout 或使用您当前的LinearLayout,但不是为每个数字/字符分配一个LinearLayout,而是您可以这样做(仅适用于第一行,但您明白了):

        `

        <LinearLayout 
            android:id="@+id/LL_topRow"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="3"
            android:layout_margin="2dp">
        
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:textSize="64sp"
                android:clickable="true"
                android:gravity="center"
                android:text="1" />
        
        
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:textSize="64sp"
                android:clickable="true"
                android:gravity="center"
                android:text="2" />
        
        
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:textSize="64sp"
                android:clickable="true"
                android:gravity="center"
                android:text="3" />
        
        </LinearLayout>
        
        <!-- Next LinearLayout Here for next row -->
        

        `

        【讨论】:

          猜你喜欢
          • 2018-11-12
          • 1970-01-01
          • 2020-08-25
          • 1970-01-01
          • 2012-08-03
          • 2019-11-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多