2012-09-07 67 views
22

我想将布局的宽度设置为“fill_parent”,同时使视图的高度只有相同的长度,以使布局成为正方形。如何在Android中为布局设置固定纵横比

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

+0

使用wrap_content。它采用与视图相同的宽度和长度。 –

+0

可能的重复[填充剩余空间与固定长宽比surfaceview](http://stackoverflow.com/questions/10510371/fill-remaining-space-with-fixed-aspect-ratio-surfaceview) – Tim

+0

我挣扎着这与我自己一会儿,最终我决定我必须重写我想以编程方式约束和设置尺寸的视图的onMeasure。查看链接的副本。 – Tim

回答

18

随着引进ConstraintLayout你不用编写任何代码一个单一的线,或者使用第三方或依赖PercentFrameLayout这是在26.0.0弃用。

下面是如何保持1中的例子:1宽高比使用ConstraintLayout布局:编辑XML文件

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

或替代的,你可以直接在布局编辑器编辑您的布局:

Layout Editor

+0

你好@Eugene,无论何时我用它来捕捉视频,宽度总是看起来有点有弹性,这可能是导致这种情况的原因。 –

+0

请分享截图。 –

+0

https://meshileya.github.io/error.png那是截图@Eugene 感谢您的回复。 –

-1

一套高度fill_parent,宽度为wrap_content

,你修复与android:adjustViewBounds="true"

+0

AdjustViewBounds仅适用于ImageView,它约束了drawable的比率,而不是视图的比率。 – Tim

+0

所以你想让布局SQUARE? –

17

长宽比你可以尝试最初设定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); 
     } 
    }); 
} 

R.id.squareView是有问题的视图的id。并且请注意,此代码被封装在onGlobalLayout调用中以确保squareView.getWidth()具有有意义的值。

+0

它的工作原理!非常感谢! – Bolton

+0

'removeGlobalOnLayoutListener'已弃用。使用'removeOnGlobalLayoutListener(...)' – Aks4125

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


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

这或多或少都可以,但是它全部取决于调用它时的*。例如,如果您要将此代码放在onCreate中,ll.getWidth()将返回0. – newbyca

19

在你的build.gradle地址:

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

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

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

然后查看或的ViewGroup要遵循的比率替换机器人:layout_width机器人:layout_height

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

和你有一个视图或ViewGroup中,其中宽高比为16:9(1.78:1)宽度匹配父母和高度相应地调整。

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

+1

我同意这里所说的一切,除非要删除您提到的正常宽度和高度属性,这一点非常重要。我已经尝试过了,但它不适合我。相反,我不得不将它们设置为以下android:layout_width =“0dp” android:layout_height =“0dp”如下所述:http://stackoverflow.com/questions/34931023/percentframelayout-you-must-supply-a- layout-width-attribute –

+0

警告:只有容器宽度> =容器高度* 16/9时,这才会起作用。例如,如果您的容器宽度为1000dp,高度为50dp,则您的孩子将适合宽度并且高度为1000 * 9/16 = 562.5。 –

+1

'PercentRelativeLayout'和'PercentFrameLayout'现在已被弃用,建议使用'ConstraintLayout'代替。 –

2

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

RatioLayouts

安装

这个添加到文件的顶部

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


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

使用

定义在布局上根视图 '应用' 命名空间

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"> 
相关问题