简体   繁体   English

Android 布局权重不适用于相对布局

[英]Android Layout Weight not working with relative layout

在此处输入图像描述

I am trying to implement layout weight for the first time, a bit I tried with linear layout it works good, but when I tried with relative and linear layout something went wrong.我是第一次尝试实现布局权重,我尝试过使用线性布局效果很好,但是当我尝试使用相对和线性布局时出现了问题。 What is wrong here?这里有什么问题?

My XML File我的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >

<RelativeLayout
    android:id="@+id/Rlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="45" >

    <Gallery
        android:id="@+id/Gallery01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:spacing="4dip" >
    </Gallery>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/Rlayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="10"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />

    <ImageView
        android:id="@+id/ImageView02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />
</RelativeLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/navbar"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="45"
    android:background="@drawable/button1"
    android:orientation="horizontal"
    android:weightSum="100" >

    <Button
        android:id="@+id/makerback"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="20"
        android:background="@drawable/makerback" />

    <Button
        android:id="@+id/makerphoto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="20"
        android:background="@drawable/makerphoto" />

    <Button
        android:id="@+id/makerselves"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="20"
        android:background="@drawable/makerselves" />

    <Button
        android:id="@+id/makerskins"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="20"
        android:background="@drawable/makerskins" />

    <Button
        android:id="@+id/makersave"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="20"
        android:background="@drawable/makersave" />
</LinearLayout>

</LinearLayout>

I need to achieve the above image:我需要实现上图:

You cannot use percentages to define the dimensions of a View inside a RelativeLayout.您不能使用百分比来定义 RelativeLayout 中视图的尺寸。 The best ways to do it is to use LinearLayout and weights, or a custom Layout.最好的方法是使用 LinearLayout 和权重,或者自定义布局。 -Romain Guy -Romain Guy

Percentage width in a RelativeLayout RelativeLayout 中的百分比宽度

From Docs: http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#weight来自文档:http: //developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#weight

UPDATE更新

We can now use PercentRelativeLayout .我们现在可以使用PercentRelativeLayout Example from docs:来自文档的示例:

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

Try this: (Please notice the change within star symbol)试试这个:(请注意星号内的变化)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="100" >

    <RelativeLayout
        android:id="@+id/Rlayout"
        android:layout_width="fill_parent"
        *android:layout_height="0dp"*
        android:layout_weight="45" >

        <Gallery
            android:id="@+id/Gallery01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:spacing="4dip" >
        </Gallery>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/Rlayout1"
        android:layout_width="fill_parent"
        *android:layout_height="0"*
        android:layout_weight="10"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY" />

        <ImageView
            android:id="@+id/ImageView02"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY" />
    </RelativeLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/navbar"
        android:layout_width="fill_parent"
        *android:layout_height="0"*
        android:layout_weight="45"
        android:background="@drawable/button1"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Button
            android:id="@+id/makerback"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="20"
            android:background="@drawable/makerback" />

        <Button
            android:id="@+id/makerphoto"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="20"
            android:background="@drawable/makerphoto" />

        <Button
            android:id="@+id/makerselves"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="20"
            android:background="@drawable/makerselves" />

        <Button
            android:id="@+id/makerskins"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="20"
            android:background="@drawable/makerskins" />

        <Button
            android:id="@+id/makersave"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="20"
            android:background="@drawable/makersave" />
    </LinearLayout>

</LinearLayout>

You don't use weight like that.你不要那样使用重量。 Try尝试

<RelativeLayout
    android:layout_height="0dip"
    android:layout_weight=1
    ... />

for example.例如。

this may be use full这可能是充分利用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"   >

    <RelativeLayout
        android:id="@+id/Rlayout"
        android:layout_width="fill_parent"
        android:layout_height="150dp"  >
        <Gallery
            android:id="@+id/Gallery01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:spacing="4dip"
            android:background="@drawable/ic_launcher">
        </Gallery>
    </RelativeLayout>

    <!-- <RelativeLayout
           android:id="@+id/Rlayout1"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"    
           android:orientation="vertical" >


       <ImageView
          android:id="@+id/ImageView01"
          android:layout_width="fill_parent"
          android:layout_height="250dp"
          android:background="@drawable/ic_launcher"
          android:scaleType="fitXY" />

     </RelativeLayout> -->

    <ImageButton
        android:id="@+id/help"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="15dp"
        android:background="@drawable/ic_launcher" />


    <ImageButton
        android:id="@+id/numbers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="16dp"
        android:layout_toLeftOf="@+id/numbers"
        android:background="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/forword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/numbers"
        android:background="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="14dp"
        android:background="@drawable/ic_launcher" />

</RelativeLayout>

You can implement it with traditional layout hierarchy but you should be aware of nested layout's negative impact on UI performance.您可以使用传统的布局层次结构来实现它,但您应该意识到嵌套布局对 UI 性能的负面影响。 Read Understanding the performance benefits of ConstraintLayout for reference.阅读了解 ConstraintLayout 的性能优势以供参考。

To flatten your layout and use percentage values you can use ConstraintLayout's guidelines:要展平您的布局并使用百分比值,您可以使用 ConstraintLayout 的指南:

布局编辑器

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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
        android:id="@+id/ImageView01"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toTopOf="@+id/horGuideline1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/ImageView02"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toTopOf="@+id/horGuideline1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Gallery
        android:id="@+id/Gallery01"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:spacing="4dip"
        app:layout_constraintBottom_toTopOf="@+id/horGuideline2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/horGuideline1" />

    <Button
        android:id="@+id/makerback"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/verGuideline1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/horGuideline2" />

    <Button
        android:id="@+id/makerphoto"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/verGuideline2"
        app:layout_constraintStart_toEndOf="@+id/verGuideline1"
        app:layout_constraintTop_toTopOf="@+id/horGuideline2" />

    <Button
        android:id="@+id/makerselves"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/verGuideline3"
        app:layout_constraintStart_toEndOf="@+id/verGuideline2"
        app:layout_constraintTop_toTopOf="@+id/horGuideline2" />

    <Button
        android:id="@+id/makerskins"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/verGuideline4"
        app:layout_constraintStart_toEndOf="@+id/verGuideline3"
        app:layout_constraintTop_toTopOf="@+id/horGuideline2" />

    <Button
        android:id="@+id/makersave"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/verGuideline4"
        app:layout_constraintTop_toTopOf="@+id/horGuideline2" />

    <android.support.constraint.Guideline
        android:id="@+id/horGuideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />

    <android.support.constraint.Guideline
        android:id="@+id/horGuideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.9" />

    <android.support.constraint.Guideline
        android:id="@+id/verGuideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />

    <android.support.constraint.Guideline
        android:id="@+id/verGuideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.4" />

    <android.support.constraint.Guideline
        android:id="@+id/verGuideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.6" />

    <android.support.constraint.Guideline
        android:id="@+id/verGuideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.8" />

</android.support.constraint.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM