如何在 Android 中为布局设置固定纵横比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12311346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:23:31  来源:igfitidea点击:

How to set fixed aspect ratio for a layout in Android

android

提问by Bolton

I am trying to set the width of the layout to "fill_parent" while having the height of the view just the same length, to make the layout a square.

我试图将布局的宽度设置为“fill_parent”,同时视图的高度与长度相同,以使布局成为正方形。

Any suggestions will be appreciate, thanks in advance! :D

任何建议将不胜感激,提前致谢!:D

回答by Eugene Brusov

With introduction of ConstraintLayoutyou don't have to write either a single line of code or use third-parties or rely on PercentFrameLayoutwhich were deprecated in 26.0.0.

随着ConstraintLayout 的引入,您不必编写一行代码或使用第三方或依赖PercentFrameLayout于 26.0.0 中已弃用的第三方。

Here's the example of how to keep 1:1 aspect ratio for your layout using ConstraintLayout:

以下是如何使用ConstraintLayout以下命令为布局保持 1:1 纵横比的示例:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:background="@android:color/black"
        app:layout_constraintDimensionRatio="H,1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

</android.support.constraint.ConstraintLayout>

Or instead of editing your XML file, you can edit your layout directly in Layout Editor:

或者,您可以直接在布局编辑器中编辑您的布局,而不是编辑您的 XML 文件:

Layout Editor

布局编辑器

回答by Espen Riskedal

In your build.gradleadd:

在你的build.gradle添加:

compile 'com.android.support:percent:23.1.1'

and in your layout.xmlwrapwhatever view or viewgroup you want to to follow a ratio inside a PercentFrameLayoutwhere:

并在您的layout.xml 中包装您想要在PercentFrameLayout中遵循比例的任何视图或视图,其中:

android:layout_width="match_parent"
android:layout_height="wrap_content"

and then for the view or viewgroup you want to to follow a ratio replaceandroid:layout_widthand android:layout_height:

然后对于您想要遵循的比例替换android:layout_widthandroid:layout_height的视图或视图组:

    app:layout_aspectRatio="178%"
    app:layout_widthPercent="100%"

and you have a view or viewgroup where the aspect ratio is 16:9 (1.78:1) with the width matching the parent and the height adjusted accordingly.

并且您有一个视图或视图组,其中纵横比为 16:9 (1.78:1),宽度与父级匹配,高度相应调整。

Note: It's important you remove the normal width and height properties. Lint will complain, but it'll work.

注意:删除正常的宽度和高度属性很重要。Lint 会抱怨,但它会起作用。

回答by newbyca

You might try initially setting the layout_heightto wrap_content. But from there I think you have to go into code. Just to experiment, in your activity try something like:

您可以尝试最初将 设置layout_heightwrap_content。但从那里我认为你必须进入代码。只是为了实验,在您的活动中尝试以下操作:

@Override
public void onResume(){
    super.onResume();
    findViewById(R.id.squareView).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override public void onGlobalLayout() {
            View squareView = findViewById(R.id.squareView);
            LayoutParams layout = squareView.getLayoutParams();
            layout.height = squareView.getWidth();
            squareView.setLayoutParams(layout);
            squareView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });
}

Where R.id.squareViewis the idof the view in question. And note, this code is wrapped in the onGlobalLayoutcall to ensure that squareView.getWidth()has a meaningful value.

R.id.squareViewid问题的视图在哪里。请注意,此代码包含在onGlobalLayout调用中以确保squareView.getWidth()具有有意义的值。

回答by Ritesh Shakya

I created a layout library for similiar use case. Feel free to use it.

我为类似的用例创建了一个布局库。随意使用它。

RatioLayouts

比例布局

Installation

安装

Add this to the top of the file

将此添加到文件的顶部

repositories {
    maven {
        url  "http://dl.bintray.com/riteshakya037/maven" 
    }
}


dependencies {
    compile 'com.ritesh:ratiolayout:1.0.0'
}

Usage

用法

Define 'app' namespace on root view in your layout

在布局中的根视图上定义“app”命名空间

xmlns:app="http://schemas.android.com/apk/res-auto"

Include this library in your layout

在您的布局中包含此库

<com.ritesh.ratiolayout.RatioRelativeLayout
        android:id="@+id/activity_main_ratio_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:fixed_attribute="WIDTH" // Fix one side of the layout 
        app:horizontal_ratio="2" // ratio of 2:3
        app:vertical_ratio="3">

回答by mohamed murashid

 final RelativeLayout rlMyList = findViewById(R.id.rlMyList);
 rlMyList.postDelayed(new Runnable() {
     @Override
     public void run() {
          int width = rlMyList.getWidth();
          LayoutParams lp = (LayoutParams) rlMyList.getLayoutParams();
          lp.width = width;
          lp.height = (int) (width * 0.67);// in my case ratio 3:2
          rlMyList.setLayoutParams(lp);
     }
 },500);

回答by Aditya Nikhade

LinearLayout ll = (LinearLayout)findViewById(R.id.LL);


int width = ll.getWidth();
LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams(width, width);
ll.setLayoutParams(ll_params);

回答by Aditya Nikhade

set height as fill_parent, the width as wrap_content

设置高度为fill_parent,宽度为wrap_content

and you fix the aspect ratio with android:adjustViewBounds="true"

然后你固定纵横比 android:adjustViewBounds="true"