android - 做动态但方形布局的简单方法
我正在使用GridView来显示一堆基本上是LinearLayouts的视图。我希望LinearLayouts都是正方形,但我也希望它们是动态大小的 - 也就是说,有两列我希望LinearLayouts到 拉伸取决于屏幕的大小,但保持方形。 有没有办法通过xml布局执行此操作,还是必须以编程方式设置高度和宽度?
9个解决方案
99 votes
方块GridView项目的整洁解决方案是扩展RelativeLayout或LinearLayout并覆盖onMeasure,如下所示:
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
jdamcd answered 2019-05-30T07:14:56Z
43 votes
xml中没有任何内容可以让您链接宽度和高度属性。 可能最简单的事情是子类LinearLayout并覆盖LinearLayout
@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size);
}
我用它来创建以前总是正方形的视图。 它应该仍适用于LinearLayout。
更多信息将有助于这样做:[http://developer.android.com/guide/topics/ui/custom-components.html][http://developer.android.com/reference/android/view/View.MeasureSpec.html]
David Merriman answered 2019-05-30T07:15:36Z
37 votes
我这样做了:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int size;
if(widthMode == MeasureSpec.EXACTLY && widthSize > 0){
size = widthSize;
}
else if(heightMode == MeasureSpec.EXACTLY && heightSize > 0){
size = heightSize;
}
else{
size = widthSize < heightSize ? widthSize : heightSize;
}
int finalMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(finalMeasureSpec, finalMeasureSpec);
}
通过此实现,您的布局将是正方形,假设宽度和高度之间的尺寸较小。 它甚至可以设置动态值,例如在LinearLayout中使用权重。
Fernando Camargo answered 2019-05-30T07:16:09Z
30 votes
回答可能会迟到但仍然会对新访客有所帮助。
借助Android Studio 2.3中引入的新ConstraintLayout,现在可以非常轻松地构建响应式布局。
在父ConstraintLayout中,要使其任何子视图/布局动态平方,请添加此属性
app:layout_constraintDimensionRatio="w,1:1"
w是指定宽度方向约束,1:1比率确保方形布局。
Himanshu Chauhan answered 2019-05-30T07:16:56Z
8 votes
我们可以用一种非常简单的方式来做 - 只需拨打super.onMeasure()两次。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
int squareLen = Math.min(width, height);
super.onMeasure(
MeasureSpec.makeMeasureSpec(squareLen, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(squareLen, MeasureSpec.EXACTLY));
}
通过两次调用super.onMeasure(),这在绘图过程方面效率较低,但这是修复其他答案可能导致的布局问题的简单方法。
passerbywhu answered 2019-05-30T07:17:29Z
3 votes
它很简单:
public class SquareRelativeLayout extends RelativeLayout {
public SquareRelativeLayout(Context context) {
super(context);
}
public SquareRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (widthMeasureSpec < heightMeasureSpec)
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
else
super.onMeasure(heightMeasureSpec, heightMeasureSpec);
}
}
Abdul Wasae answered 2019-05-30T07:17:51Z
1 votes
我的建议是创建一个继承自FrameLayout的自定义布局类。 重写OnMeasure()方法,并在SquareFrameLayout中放置您想要的任何控件。
这是在Xamarin.Android中完成的:
public class SquareFrameLayout : FrameLayout
{
private const string _tag = "SquareFrameLayout";
public SquareFrameLayout(Android.Content.Context context):base(context) {}
public SquareFrameLayout(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer):base(javaReference, transfer) {}
public SquareFrameLayout(Android.Content.Context context, IAttributeSet attrs):base(context, attrs) {}
public SquareFrameLayout(Android.Content.Context context, IAttributeSet attrs, int defStyleAttr):base(context, attrs, defStyleAttr) {}
public SquareFrameLayout(Android.Content.Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes):base(context, attrs, defStyleAttr, defStyleRes) {}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
var widthMode = MeasureSpec.GetMode(widthMeasureSpec);
int widthSize = MeasureSpec.GetSize(widthMeasureSpec);
var heightMode = MeasureSpec.GetMode(heightMeasureSpec);
int heightSize = MeasureSpec.GetSize(heightMeasureSpec);
int width, height;
switch (widthMode)
{
case MeasureSpecMode.Exactly:
width = widthSize;
break;
case MeasureSpecMode.AtMost:
width = Math.Min(widthSize, heightSize);
break;
default:
width = 100;
break;
}
switch (heightMode)
{
case MeasureSpecMode.Exactly:
height = heightSize;
break;
case MeasureSpecMode.AtMost:
height = Math.Min(widthSize, heightSize);
break;
default:
height = 100;
break;
}
Log.Debug(_tag, $"OnMeasure({widthMeasureSpec}, {heightMeasureSpec}) => Width mode: {widthMode}, Width: {widthSize}/{width}, Height mode: {heightMode}, Height: {heightSize}/{height}");
var size = Math.Min(width, height);
var newMeasureSpec = MeasureSpec.MakeMeasureSpec(size, MeasureSpecMode.Exactly);
base.OnMeasure(newMeasureSpec, newMeasureSpec);
}
}
如果您希望View(或任何其他控件)为方形(并居中),只需按以下方式将其添加到布局中:
android:id="@+id/squareContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
android:id="@+id/squareContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
sdippl answered 2019-05-30T07:18:32Z
0 votes
这是一个适用于所有可设置为查看或查看组的布局参数的解决方案:
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int widthDesc = MeasureSpec.getMode(widthMeasureSpec);
int heightDesc = MeasureSpec.getMode(heightMeasureSpec);
int size = 0;
if (widthDesc == MeasureSpec.UNSPECIFIED
&& heightDesc == MeasureSpec.UNSPECIFIED) {
size = DP(defaultSize); // Use your own default size, in our case
// it's 125dp
} else if ((widthDesc == MeasureSpec.UNSPECIFIED || heightDesc == MeasureSpec.UNSPECIFIED)
&& !(widthDesc == MeasureSpec.UNSPECIFIED && heightDesc == MeasureSpec.UNSPECIFIED)) {
//Only one of the dimensions has been specified so we choose the dimension that has a value (in the case of unspecified, the value assigned is 0)
size = width > height ? width : height;
} else {
//In all other cases both dimensions have been specified so we choose the smaller of the two
size = width > height ? height : width;
}
setMeasuredDimension(size, size);
干杯
Zaid Daghestani answered 2019-05-30T07:19:01Z
0 votes
查看SquareLayout,这是一个为不同的Layouts提供包装类的Android库,使它们在不丢失任何核心功能的情况下呈现Squared尺寸。
尺寸是在渲染布局之前计算的,因此一旦获得视图,就不会重新渲染或进行任何调整。
要使用库,请将其添加到build.gradle:
repositories {
maven {
url "https://maven.google.com"
}
}
dependencies {
compile 'com.github.kaushikthedeveloper:squarelayout:0.0.3'
}
您需要的是SquareLinearLayout。
Kaushik NP answered 2019-05-30T07:19:50Z