将文本视图与约束布局对齐
Aligning Text View with constraint Layout
我正在尝试在不使用边距的情况下在约束布局中对齐我的 textview,以便布局可以在所有设备上响应,但到目前为止我一直在定位。这是我的代码和图像中附带的预期输出。我只是想让金额与货币文本稍微对齐。
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintEnd_toEndOf="@+id/yearBarChartAmount"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="@id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="RM"/>
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tOTAL"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toEndOf="@id/yearBarChartCurrency"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="233.34"/>
</android.support.constraint.ConstraintLayout>
您可以使用准则来实现此目的:
<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="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="balance"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="233.34"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
看起来像这样:
我必须强调一件真正困扰我的事情:
“边距使布局可以在所有设备上响应” - 这不是真的,让我解释一下。
让你的屏幕响应所有屏幕尺寸的原因是 constraintLayout 以及你如何使用它,我同意 dp
中的大量边距会使你的屏幕不响应 但是 建议在 google material 设计中使用较小的边距 - 它实际上会让您的应用程序看起来更好,并防止您的视图直接附加到父边框。
我假设您想对齐文本的顶部。
A TextView
具有基于字体排版的内部结构。 "Android 101: Typography" has a good explanation of Android typography. This diagram 特别有用。
因此,以下布局在设计视图中看起来像这样。如您所见,文本的顶部没有对齐。
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4778C5"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.7"
android:text="Balance"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="@+id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@+id/year_bar_chart_total"
tools:text="RM" />
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="4sp"
android:paddingLeft="4sp"
android:text="233.34"
android:textColor="@android:color/white"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/yearBarChartCurrency"
app:layout_constraintTop_toTopOf="@+id/yearBarChartCurrency" />
</android.support.constraint.ConstraintLayout>
尽管 TextViews
的字体相同,但不同的字体大小会阻止实际文本的顶部对齐,即使 TextViews
的顶部对齐。这是因为指标不同。
因此,要对齐文本的 实际 顶部,我们需要确定实际文本开始的 TextView
顶部下方多远,并将文本移动这些金额。以下代码执行此操作。注释在代码中。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView yearBarChartAmount = findViewById(R.id.yearBarChartAmount);
TextView yearBarChartCurrency = findViewById(R.id.yearBarChartCurrency);
yearBarChartCurrency.setTranslationY(-getTopOfText(yearBarChartCurrency));
yearBarChartAmount.setTranslationY(-getTopOfText(yearBarChartAmount));
}
private int getTopOfText(TextView textView) {
// Force measure of text pre-layout.
textView.measure(0, 0);
// bounds will store the rectangle that will circumscribe the text.
Rect bounds = new Rect();
// Get the bounds for the text. Top and bottom are measured from the baseline. Left
// and right are measured from 0.
String text = (String) textView.getText();
textView.getPaint().getTextBounds(text, 0, text.length(), bounds);
return textView.getBaseline() + bounds.top;
}
}
显示内容如下:
可能还有其他考虑,但这是解决方案的要点。将其封装到自定义 TextView
.
中可能是值得的
我正在尝试在不使用边距的情况下在约束布局中对齐我的 textview,以便布局可以在所有设备上响应,但到目前为止我一直在定位。这是我的代码和图像中附带的预期输出。我只是想让金额与货币文本稍微对齐。
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintEnd_toEndOf="@+id/yearBarChartAmount"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="@id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="RM"/>
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tOTAL"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toEndOf="@id/yearBarChartCurrency"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="233.34"/>
</android.support.constraint.ConstraintLayout>
您可以使用准则来实现此目的:
<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="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="balance"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="233.34"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
看起来像这样:
我必须强调一件真正困扰我的事情:
“边距使布局可以在所有设备上响应” - 这不是真的,让我解释一下。
让你的屏幕响应所有屏幕尺寸的原因是 constraintLayout 以及你如何使用它,我同意 dp
中的大量边距会使你的屏幕不响应 但是 建议在 google material 设计中使用较小的边距 - 它实际上会让您的应用程序看起来更好,并防止您的视图直接附加到父边框。
我假设您想对齐文本的顶部。
A TextView
具有基于字体排版的内部结构。 "Android 101: Typography" has a good explanation of Android typography. This diagram 特别有用。
因此,以下布局在设计视图中看起来像这样。如您所见,文本的顶部没有对齐。
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4778C5"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.7"
android:text="Balance"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="@+id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@+id/year_bar_chart_total"
tools:text="RM" />
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="4sp"
android:paddingLeft="4sp"
android:text="233.34"
android:textColor="@android:color/white"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/yearBarChartCurrency"
app:layout_constraintTop_toTopOf="@+id/yearBarChartCurrency" />
</android.support.constraint.ConstraintLayout>
尽管 TextViews
的字体相同,但不同的字体大小会阻止实际文本的顶部对齐,即使 TextViews
的顶部对齐。这是因为指标不同。
因此,要对齐文本的 实际 顶部,我们需要确定实际文本开始的 TextView
顶部下方多远,并将文本移动这些金额。以下代码执行此操作。注释在代码中。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView yearBarChartAmount = findViewById(R.id.yearBarChartAmount);
TextView yearBarChartCurrency = findViewById(R.id.yearBarChartCurrency);
yearBarChartCurrency.setTranslationY(-getTopOfText(yearBarChartCurrency));
yearBarChartAmount.setTranslationY(-getTopOfText(yearBarChartAmount));
}
private int getTopOfText(TextView textView) {
// Force measure of text pre-layout.
textView.measure(0, 0);
// bounds will store the rectangle that will circumscribe the text.
Rect bounds = new Rect();
// Get the bounds for the text. Top and bottom are measured from the baseline. Left
// and right are measured from 0.
String text = (String) textView.getText();
textView.getPaint().getTextBounds(text, 0, text.length(), bounds);
return textView.getBaseline() + bounds.top;
}
}
显示内容如下:
可能还有其他考虑,但这是解决方案的要点。将其封装到自定义 TextView
.