【问题标题】:How to divide linear layout into four equal parts?如何将线性布局分成四个相等的部分?
【发布时间】:2020-11-22 15:18:52
【问题描述】:

我想给 Textview 3/4 的线性布局和 imagebutton 视图 1/4。但它没有按预期工作,但如果我制作四个按钮,它会平分它。

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">
    <TextView
        android:background="@color/dark"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/txtresult"
        android:text=""
        android:textColor="@color/colorPrimary"
        android:textSize="50sp"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/del"
        android:text="@string/del"
        android:autoSizeTextType="uniform"
        style="@style/Widget.MaterialComponents.Button"
        android:textColor="#fff"
        android:layout_weight="1"
        android:src="@drawable/ic_action_name"
        android:scaleType="fitCenter" />

    </LinearLayout>

【问题讨论】:

  • &lt;TextView&gt;&lt;ImageView&gt;layout_widths 更改为0dp。此外,&lt;LinearLayout&gt; 上的 layout_weight 不是必需的,除非它实际上是在另一个 &lt;LinearLayout&gt; 中加权自身。

标签: xml android-studio android-layout android-linearlayout android-layout-weight


【解决方案1】:

从“LinearLayout”标签中删除“layout_weight”它为我工作。

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/txtresult"
        android:text=""
        android:textColor="@color/colorPrimary"
        android:textSize="50sp"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/del"
        android:autoSizeTextType="uniform"
        android:textColor="#fff"
        android:layout_weight="1"
        android:scaleType="fitCenter" />
</LinearLayout>

【讨论】:

    【解决方案2】:

    首先从 LinearLayout 中删除 android:layout_weight="1"。然后为 TextView 和 ImageButton 设置 android:layout_width="0dp"。然后为 TextView 设置 android:layout_weight="0.75" 并为 ImageButton 设置 android:layout_weight="0.25"

    像这样:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:background="@color/dark"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.75"
        android:id="@+id/txtresult"
        android:text=""
        android:textColor="@color/colorPrimary"
        android:textSize="50sp"/>
    <ImageButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/del"
        android:text="@string/del"
        android:autoSizeTextType="uniform"
        style="@style/Widget.MaterialComponents.Button"
        android:textColor="#fff"
        android:layout_weight="0.25"
        android:src="@drawable/ic_action_name"
        android:scaleType="fitCenter" />
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      相关资源
      最近更新 更多