我想将布局的宽度设置为“fill_parent”,而视图的高度保持相同的长度,使布局成为一个正方形。
欢迎提出任何建议。
我想将布局的宽度设置为“fill_parent”,而视图的高度保持相同的长度,使布局成为一个正方形。
欢迎提出任何建议。
引入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 文件:
在您的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会抱怨,但它会起作用。
PercentRelativeLayout
和PercentFrameLayout
现已被弃用,建议改用ConstraintLayout
。 - advice你可以尝试将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
is deprecated. use removeOnGlobalLayoutListener(...)
- Aks4125将此添加到文件顶部。
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">
<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>
class FixedAspectRatioView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val width = MeasureSpec.getSize(widthMeasureSpec)
val height = (width / 1.618).roundToInt()
val newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
super.onMeasure(widthMeasureSpec, newHeightMeasureSpec)
}
}
val width = MeasureSpec.getSize(widthMeasureSpec)
: 这行代码使用MeasureSpec.getSize()
从widthMeasureSpec
中获取可用的宽度。这个宽度将用于根据所需的宽高比计算相应的高度。val height = (width / 1.618).roundToInt()
: 这行代码通过将可用宽度(width
)除以所需的宽高比(1.618)来计算所需的高度。使用roundToInt()
将结果四舍五入为最接近的整数。val newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
: 这行代码使用MeasureSpec.makeMeasureSpec()
创建一个新的高度测量规范(newHeightMeasureSpec
)。高度设置为计算得到的height
,模式设置为MeasureSpec.EXACTLY
,表示高度应该完全按照指定的值。super.onMeasure(widthMeasureSpec, newHeightMeasureSpec)
: 这行代码使用原始的widthMeasureSpec
和新的newHeightMeasureSpec
调用父类的onMeasure()
方法。这确保视图使用所需的宽度和高度进行测量,保持固定的宽高比。 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);
LinearLayout ll = (LinearLayout)findViewById(R.id.LL);
int width = ll.getWidth();
LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams(width, width);
ll.setLayoutParams(ll_params);
将高度设置为fill_parent
,宽度设置为wrap_content
并使用android:adjustViewBounds="true"
来固定宽高比。