两个垂直线性布局,并希望一个比另一个宽

时间:2018-05-23 13:11:01

标签: android-studio

我有一个布局,它是一个包含2个垂直线性布局的relativeLayout。每个线性布局使用相等的宽度。方向是横向,我想使第一个线性布局使用2/3的布局,第二个linearlayout使用另一个1/3。 我不熟悉XML。我的背景是Visual Basic,现在使用Android Studio转移到Java。

以下是活动布局的XML。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
        android:layout_weight="3"
        android:background="@color/colorBackgroundGray"
        tools:layout="@layout/fragment_select_item"
        tools:context="com.myCompany.myProduct.activities.ReturnAreaActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        >
        <FrameLayout
            android:id="@+id/FragmentFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


        </FrameLayout>
        <include
            android:id="@+id/include2"
            layout="@layout/frm1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="left"
            android:layout_weight="1.7" />



    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:paddingLeft="5dp"
        android:orientation="vertical">

        <include
            android:id="@+id/include"
            layout="@layout/layout_numpad5"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="220dp"
            android:layout_weight="1.3"
            android:layout_gravity="right"
            />

    </LinearLayout>



</RelativeLayout>

重量是我刚试过的。有任何想法吗? enter image description here

1 个答案:

答案 0 :(得分:0)

尝试以下基本逻辑,它可以正常使用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="First Linear Layout"
        android:gravity="center"/>

</LinearLayout>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Second Linear Layout"
        android:gravity="center"/>

</LinearLayout>

</LinearLayout>
  

输出:

enter image description here

  

要改变方向(如果你不要并排布局)只需更改值或android:orientation =“vertical”到android:orientation =“horizo​​ntal”。

     

输出:

enter image description here