我试图将布局的宽度设置为“fill_parent”,同时让视图的高度与长度相同,以使布局成为正方形。
任何建议将不胜感激,在此先感谢!:D
随着ConstraintLayout的引入,您不必编写任何代码或使用第三方或依赖PercentFrameLayout
于 26.0.0 中已弃用的第三方。
以下是如何使用以下方法为您的布局保持 1:1 纵横比的示例ConstraintLayout
:
<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 文件:
在你的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 会抱怨,但它会起作用。
您可以尝试最初将 设置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()
具有有意义的值。
我为类似的用例创建了一个布局库。随意使用它。
将此添加到文件顶部
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">
LinearLayout ll = (LinearLayout)findViewById(R.id.LL);
int width = ll.getWidth();
LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams(width, width);
ll.setLayoutParams(ll_params);
如果你想保持纵横比并且父布局必须是“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>
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);
设置高度为fill_parent
,宽度为wrap_content
你用android:adjustViewBounds="true"