【问题标题】:How to set fixed aspect ratio for a layout in Android如何在Android中为布局设置固定纵横比
【发布时间】:2012-08-31 22:31:12
【问题描述】:

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

任何建议将不胜感激,在此先感谢! :D

【问题讨论】:

标签: android


【解决方案1】:

设置高度为fill_parent,宽度设置为wrap_content

然后你用android:adjustViewBounds="true"修复纵横比

【讨论】:

  • AdjustViewBounds只适用于ImageView,它限制drawable的比例,而不是view的比例。
  • 所以你想让布局 SQUARE 吗?
【解决方案2】:

您可以尝试最初将layout_height 设置为wrap_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);
        }
    });
}

其中R.id.squareView 是相关视图的id。请注意,此代码包含在 onGlobalLayout 调用中,以确保 squareView.getWidth() 具有有意义的值。

【讨论】:

  • removeGlobalOnLayoutListener 已弃用。使用removeOnGlobalLayoutListener(...)
【解决方案3】:
LinearLayout ll = (LinearLayout)findViewById(R.id.LL);


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

【讨论】:

  • 这或多或少没问题,但这一切都取决于 何时 它被调用。例如,如果您将此代码放在 onCreate 中,ll.getWidth() 将返回 0。
【解决方案4】:

在你的 build.gradle 添加:

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

在您的 layout.xml wrap 中您想要遵循PercentFrameLayout 内的比率的任何视图或视图组,其中:

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

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

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

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

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

【讨论】:

  • 我同意这里所说的一切,除了删除您提到的正常宽度和高度属性很重要。我已经尝试过了,但它对我不起作用。相反,我必须将它们设置为以下 android:layout_width="0dp" android:layout_height="0dp" ,如下所述:stackoverflow.com/questions/34931023/…
  • 警告:这仅在容器宽度 >= 容器高度 * 16/9 时有效。例如,如果您的容器宽度为 1000dp,高度为 50dp,您的孩子将适合宽度,高度为 1000*9/16=562.5。
  • PercentRelativeLayoutPercentFrameLayout 现已弃用,建议改用 ConstraintLayout
【解决方案5】:

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

RatioLayouts

安装

将此添加到文件顶部

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


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

用法

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

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

在你的布局中包含这个库

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

【讨论】:

  • 现在已弃用
【解决方案6】:

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

以下是如何使用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>

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

【讨论】:

  • 你好@Eugene,每当我用它来捕捉视频时,宽度总是看起来有点弹性,请问这可能是什么原因。
  • 请分享截图。
  • meshileya.github.io/error.png 这是截图@Eugene 感谢您的回复。
  • 在尝试访问该页面时得到 404
  • meshileya.github.io/error.jpg 它应该是一个 .jpg 文件。非常抱歉这个错误。
【解决方案7】:
 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);

【讨论】:

    【解决方案8】:

    如果你想保持纵横比并且父布局必须是“wrap_content”(对于带有可变文本的文本视图很有用),你可以添加一个假视图,它保持纵横比并且其他元素必须限制在这个视图中:

        <androidx.constraintlayout.widget.ConstraintLayout     
            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="wrap_content"
            android:layout_height="wrap_content">
    
        <View
            android:id="@+id/ratio_constraint"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="122:246"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="@id/ratio_constraint"
            app:layout_constraintStart_toStartOf="@id/ratio_constraint"
            app:layout_constraintTop_toTopOf="@id/ratio_constraint"
            tools:text="The TextView which can extend the layout" />
    
      </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 2020-04-13
      • 1970-01-01
      • 2017-05-07
      • 2011-12-19
      • 1970-01-01
      相关资源
      最近更新 更多