我试图将布局的宽度设置为“fill_parent”,同时使视图的高度只有相同的长度,以使布局成为正方形。
任何建议都会有所体会,谢谢! :D
答案 0 :(得分:58)
通过引入,您不必编写单行代码或使用第三方,也不必依赖于{26}中弃用的
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文件,而是直接在布局编辑器中编辑布局:
{{3}}
答案 1 :(得分:19)
在 build.gradle 中添加:
compile 'com.android.support:percent:23.1.1'
并在您的 layout.xml 换行您想要跟踪PercentFrameLayout内的比率的任何视图或视图组:
android:layout_width="match_parent"
android:layout_height="wrap_content"
然后对于视图或视图组,您要遵循比率替换 android:layout_width 和 android:layout_height :
app:layout_aspectRatio="178%"
app:layout_widthPercent="100%"
并且您有一个视图或视图组,其宽高比为16:9(1.78:1),宽度与父级相匹配,并相应调整高度。
注意:删除正常的宽度和高度属性非常重要。 Lint会抱怨,但它会起作用。
答案 2 :(得分:17)
您可以尝试将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()
具有有意义的值。
答案 3 :(得分:3)
我为类似的用例创建了一个布局库。随意使用它。
将其添加到文件顶部
repositories {
maven {
url "http://dl.bintray.com/riteshakya037/maven"
}
}
dependencies {
compile 'com.ritesh:ratiolayout:1.0.0'
}
定义应用程序&#39;布局中根视图上的命名空间
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">
答案 4 :(得分: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);
答案 5 :(得分:0)
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);
答案 6 :(得分:0)
如果要保留长宽比,并且父级布局必须为“ wrap_content”(用于带有可变文本的文本视图),则可以添加一个伪造的视图,以保留长宽比,并且必须将其他元素限制为该视图:< / p>
<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>
答案 7 :(得分:-1)
将身高设为fill_parent
,宽度设为wrap_content
并使用android:adjustViewBounds="true"