在我的布局中,我有两个#borderless-cell { border: none!important; }
水平相同的权重与约束布局,但我希望那些textview
具有不同的权重,比例为6:4。
所以如何使用约束布局在我的布局中实现这一点。
答案 0 :(得分:41)
创建水平链,然后使用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>
创建视图并将其添加到父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);
然后创建一个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);
答案 1 :(得分:5)
经过一些研究后,我用指南视图来解决其他解决方案。
<?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>
答案 2 :(得分:-1)
要获得更好的体验并支持RTL和LTR,请使用开始/结束而不是向左/向右:
<?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"
layout_constraintStart_toStartOf="parent"
layout_constraintEnd_toStartOf="@+id/two"
app:layout_constraintHorizontal_weight="6"/>
<TextView
android:id="@+id/two"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="#fac"
layout_constraintEnd_toEndOf="@+id/one"
layout_constraintStart_toEndOf="parent"
app:layout_constraintHorizontal_weight="4"/>
</android.support.constraint.ConstraintLayout>`
答案 3 :(得分:-4)
您可以使用app:layout_constraintHorizontal_weight