android layout weight %

2019-07-15 17:41发布

I am trying to messing around with the Android layout weight attributes. I want a vertical layout with "3 child layouts" The first will take 25% of the space, the second 50%, and the last 25% of the space. When I try to add to the last layout, everything just not working. What should be the weight for each of these layouts?

I get the weight attributes to work fine with just 2 layouts/elements and understand how it works but when I try with 3 I get a problem.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:orientation="horizontal"
    android:weightSum="100" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical"
        android:layout_weight="25" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical"
       android:layout_weight="50" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical"
        android:layout_weight="25" >
    </LinearLayout>
</LinearLayout>

5条回答
三岁会撩人
2楼-- · 2019-07-15 17:48

Well, first, weight can only be applied to LinearLayouts or any layout that derives from LinearLayout. The weights of the children should be a percentage of whatever the weightSum attribute is for LinearLayout. If you set weightSum=1 of your parent LinearLayout, then the percentage of your first child should be .25, second child should be .5, and third child should be .25.

EDIT: Like javram said. You need to set either the width or height to 0dp depending on whether the LinearLayout is horizontal orientation or vertical orientation respectively.

EDIT 2: Also, I don't think you have to actually specify weightSum. It helps, but I believe LinearLayout will divide the children up base solely on how their own respective weights are. If child 1's weight is twice as large as child 2's weight, then child 2 will be double child 1 on screen. Thus in your case just make sure child 1 & 3 have the same weight while child 2 has double the value the other two.

查看更多
唯我独甜
3楼-- · 2019-07-15 17:51
LinearLayout java1,java2,java3;

    java1=(LinearLayout)findViewById(R.id.java1);
    java2=(LinearLayout)findViewById(R.id.java2);
    java3=(LinearLayout)findViewById(R.id.java3);
    DisplayMetrics ds=new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(ds);
    int height=ds.heightPixels;
    Log.d(TAG,""+height);

    java1.getLayoutParams().height=height/4;
    java2.getLayoutParams().height=height/2;
    java3.getLayoutParams().height=height/4;
查看更多
走好不送
4楼-- · 2019-07-15 18:02

Make sure that the android:width attribute is set to 0dp for all child layouts, and then make sure that you have a weight specified. If you want a more detailed explanation, you will need to paste your code.

查看更多
唯我独甜
5楼-- · 2019-07-15 18:05

try using this code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:weightSum="100">
  <LinearLayout
     android:layout_width="0dp"
     android:layout_height="100dp"
     android:orientation="vertical"
     android:layout_weight="25">
  </LinearLayout>
  <LinearLayout
     android:layout_width="0dp"
     android:layout_height="100dp"
     android:orientation="vertical"
     android:layout_weight="50" >
  </LinearLayout>
  <LinearLayout
     android:layout_width="0dp"
     android:layout_height="100dp"
     android:orientation="vertical"
     android:layout_weight="25">
  </LinearLayout>
</LinearLayout>
查看更多
该账号已被封号
6楼-- · 2019-07-15 18:11

There is another way of achieving this result using less complicated layout tree. You may use PercentRelativeLayout from Percent Support Library for Android. Just import this library to your project, and check out the following code:

<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">

    <LinearLayout
        android:layout_height="100dp"
        android:layout_toLeftOf="@+id/middle"
        android:layout_weight="25"
        android:orientation="vertical"
        app:layout_widthPercent="25%">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_weight="50"
        android:orientation="vertical"
        app:layout_widthPercent="50%">
    </LinearLayout>

    <LinearLayout
        android:layout_height="100dp"
        android:layout_toRightOf="@+id/middle"
        android:layout_weight="25"
        android:orientation="vertical"
        app:layout_widthPercent="25%">
    </LinearLayout>

</android.support.percent.PercentRelativeLayout>
查看更多
登录 后发表回答