回答
随着引进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>
或替代的,你可以直接在布局编辑器编辑您的布局:
你好@Eugene,无论何时我用它来捕捉视频,宽度总是看起来有点有弹性,这可能是导致这种情况的原因。 –
请分享截图。 –
https://meshileya.github.io/error.png那是截图@Eugene 感谢您的回复。 –
一套高度fill_parent
,宽度为wrap_content
,你修复与android:adjustViewBounds="true"
AdjustViewBounds仅适用于ImageView,它约束了drawable的比率,而不是视图的比率。 – Tim
所以你想让布局SQUARE? –
长宽比你可以尝试最初设定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()
具有有意义的值。
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. – newbyca
在你的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)宽度匹配父母和高度相应地调整。
注意:删除正常的宽度和高度属性很重要。林特会抱怨,但它会工作。
我同意这里所说的一切,除非要删除您提到的正常宽度和高度属性,这一点非常重要。我已经尝试过了,但它不适合我。相反,我不得不将它们设置为以下android:layout_width =“0dp” android:layout_height =“0dp”如下所述:http://stackoverflow.com/questions/34931023/percentframelayout-you-must-supply-a- layout-width-attribute –
警告:只有容器宽度> =容器高度* 16/9时,这才会起作用。例如,如果您的容器宽度为1000dp,高度为50dp,则您的孩子将适合宽度并且高度为1000 * 9/16 = 562.5。 –
'PercentRelativeLayout'和'PercentFrameLayout'现在已被弃用,建议使用'ConstraintLayout'代替。 –
我为类似用例创建了一个布局库。随意使用它。
安装
这个添加到文件的顶部
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">
- 1. 如何在matplotlib中设置纵横比?
- 2. 基于纵横比的Android布局?
- 3. Android:为布局设置固定高度?
- 4. Android布局如何设置特定的固定布局高度
- 5. Sympy:设置纵横比
- 6. 辫形预置“固定的:纵横比”属性为选择
- 7. 如何在横向布局中包含纵向布局?
- 8. 如何在Android网格视图中设置自定义纵横比?
- 9. 如何使用Spyder为Python中的绘图设置纵横比?
- 10. 如何以横向适应纵向图像在纵横比保持纵横比
- 11. 如何为纵向和横向定义不同的布局?
- 12. 如何在单页布局中设置固定位置图像
- 13. 纵横比自定义列表视图中的纵横比
- 14. 设置三维图的纵横比
- 15. Android纵横比列表?
- 16. 如何在android中设置此布局?
- 17. 保持流体CSS网格布局中元素的纵横比
- 18. 如何使用自动布局在两个视图之间设置空间(在纵横比)
- 19. 如何使用自动布局为纵向和横向定向配置不同的布局?
- 20. 如何使用造纸宝石将纵横比设置为无限制比例?
- 21. 在android中设置布局
- 22. 如何为纵向和横向布局创建UIView
- 23. android多屏支持设置方向和纵横比
- 24. android - 如何在Android 3.0中设置PreferenceActivity的自定义布局?
- 25. 如何将screenOrientation设置为清单中的横向时,在对话框活动中使用纵向布局?
- 26. 在android布局中固定页脚
- 27. 为纵向和横向定义了不同的布局?
- 28. 如何使用vba将Powerpoint 2007中的图像纵横比设置为100%?
- 29. Android的布局固定
- 30. 在OpenGL(-ES)中设置纵横比,得到奇怪的值
使用wrap_content。它采用与视图相同的宽度和长度。 –
可能的重复[填充剩余空间与固定长宽比surfaceview](http://stackoverflow.com/questions/10510371/fill-remaining-space-with-fixed-aspect-ratio-surfaceview) – Tim
我挣扎着这与我自己一会儿,最终我决定我必须重写我想以编程方式约束和设置尺寸的视图的onMeasure。查看链接的副本。 – Tim