Pra*_*jal 25 layout android android-constraintlayout
在我的布局中,我有2个textview水平相同的重量和约束布局,但我希望那些textview具有不同重量的6:4比例.
那么如何使用约束布局在我的布局中实现这一点.
Ben*_* P. 61
创建一个水平链,然后使用该app:layout_constraintHorizontal_weight属性:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="#caf"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/two"
app:layout_constraintHorizontal_weight="6"/>
<TextView
android:id="@+id/two"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="#fac"
app:layout_constraintLeft_toRightOf="@+id/one"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_weight="4"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
创建视图并将其添加到父视图ConstraintLayout.你需要给他们每个人id以便一切顺利; 您可以使用View.generateViewId()或者您可以id为它们定义资源.
// this will be MATCH_CONSTRAINTS width and 48dp height
int height = (int) (getResources().getDisplayMetrics().density * 48);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(0, height);
View left = new View(this);
left.setId(R.id.one);
parent.addView(left, params);
View right = new View(this);
right.setId(R.id.two);
parent.addView(right, params);
Run Code Online (Sandbox Code Playgroud)
然后创建一个ConstraintSet对象并创建您的链:
ConstraintSet set = new ConstraintSet();
set.clone(parent);
int[] chainIds = { R.id.one, R.id.two }; // the ids you set on your views above
float[] weights = { 6, 4 };
set.createHorizontalChain(ConstraintSet.PARENT_ID, ConstraintSet.LEFT,
ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,
chainIds, weights, ConstraintSet.CHAIN_SPREAD);
set.applyTo(parent);
Run Code Online (Sandbox Code Playgroud)
Kar*_*a_M 10
您还可以使用它app:layout_constraintWidth_percent来实现您想要的视图。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.6"
android:text="6"
android:textStyle="bold"
android:textAlignment="center"
android:paddingTop="15dp"
android:background="#ffb7b7"/>
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintStart_toEndOf="@id/textView1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.4"
android:text="4"
android:textStyle="bold"
android:textAlignment="center"
android:paddingTop="15dp"
android:background="#b7b8ff"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
以下是上述布局的屏幕
如果您需要将视图置于垂直方向,那么您可以在这种情况下使用app:layout_constraintHeight_percent并放置android:layout_height为 0dp。
经过一番研究,我找到了具有指导性观点的其他解决方案。
<?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/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/green_color"
android:fontFamily="@font/overpass_light"
android:textAllCaps="true"
android:textColor="@color/dark_grey_button"
android:textSize="@dimen/h5"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/grey_text"
android:fontFamily="@font/overpass_light"
android:textAllCaps="true"
android:textColor="@color/dark_grey_button"
android:textSize="@dimen/h5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6"
/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15089 次 |
| 最近记录: |