我需要为左侧的行创建一个带有标签的方形网格。网格应使用设备上的全部可用空间,同时保持宽度=高度。因为这个网格有几行和几列自动调整,我认为使用自定义ConstraintLayout
会满足这个要求。
在下面的代码中,它用于“纵向”布局,如果我将 SCFrame 视图的 layout_width 设置为
match_parent
: 格子的大小和预期的一样,但是and字段gravity
的并没有应用textView10
textView11
wrap_content
: 网格折叠到 0dpx0dp0dp
:与 相同wrap_content
,在我看来,这与ConstraintLayout
“使用 0dp,相当于MATCH_CONSTRAINT
”的文档不符xxxdp
: SCFrame 网格是正方形,width=xxxdp 和 height=xxxdp,and字段gravity
的应用正确textView10
textView11
我应该在以下代码中更改什么以获得一个正方形的网格并使用屏幕的整个宽度(无论使用什么设备并为标签列留出空间)并且gravity
标签正确?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/scMainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_optimizationLevel="chains"
tools:context=".MainActivity"
tools:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/textView10"
android:layout_width="14dp"
android:layout_height="0dp"
android:gravity="center_vertical"
android:text="A"
android:background="#FF0000"
app:layout_constraintTop_toTopOf="@+id/SCFrame"
app:layout_constraintRight_toLeftOf="@+id/SCFrame"
app:layout_constraintBottom_toTopOf="@+id/textView11" />
<TextView
android:id="@+id/textView11"
android:layout_width="14dp"
android:layout_height="0dp"
android:gravity="center_vertical"
android:text="B"
android:background="#FF0000"
app:layout_constraintTop_toBottomOf="@+id/textView10"
app:layout_constraintRight_toLeftOf="@+id/SCFrame"
app:layout_constraintBottom_toBottomOf="@+id/SCFrame" />
<be.ema.sclibrary.SCConstraintLayout
android:id="@+id/SCFrame"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:background="#0000FF"
app:layout_constraintLeft_toRightOf="@+id/textView10"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="@+id/A1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="this is A1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@+id/A2"
app:layout_constraintBottom_toTopOf="@+id/B1" />
<TextView
android:id="@+id/A2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="this is A2"
app:layout_constraintLeft_toRightOf="@+id/A1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/B2" />
<TextView
android:id="@+id/B1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="this is B1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/A1"
app:layout_constraintRight_toLeftOf="@+id/B2"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/B2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="this is B2"
app:layout_constraintLeft_toRightOf="@+id/B1"
app:layout_constraintTop_toBottomOf="@+id/A1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</be.ema.sclibrary.SCConstraintLayout>
</android.support.constraint.ConstraintLayout>
这是自定义 ConstraintLayout 的代码:
class SCConstraintLayout extends android.support.constraint.ConstraintLayout {
public SCConstraintLayout(Context context) {
super(context);
}
public SCConstraintLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SCConstraintLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int chosenDimension = 0;
int mode = 0;
if (ScMain.isLandscapeOriented) {
chosenDimension = MeasureSpec.getSize(heightMeasureSpec);
mode = MeasureSpec.getMode(heightMeasureSpec);
} else {
chosenDimension = MeasureSpec.getSize(widthMeasureSpec);
mode = MeasureSpec.getMode(widthMeasureSpec);
}
int width = MeasureSpec.makeMeasureSpec(chosenDimension, mode);
int height = MeasureSpec.makeMeasureSpec(chosenDimension, mode);
// setMeasuredDimension(width, height);
super.onMeasure(width, height);
}
}