Полагаю, вы хотите выровнять верх текста.
A TextView
имеет внутреннюю структуру, основанную на типографии шрифта. «Android 101: Типография» содержит хорошее объяснение типографии Android. Эта диаграмма особенно полезна.
Итак, следующий макет выглядит так в режиме конструктора. Как видите, верхние части текста не совпадают.
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);
String s = (String) textView.getText();
// bounds will store the rectangle that will circumscribe the text.
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
// Get the bounds for the text. Top and bottom are measured from the baseline. Left
// and right are measured from 0.
textPaint.getTextBounds(s, 0, s.length(), bounds);
int baseline = textView.getBaseline();
bounds.top = baseline + bounds.top;
return bounds.top;
}
}
Отображается следующее:
Могут быть и другие соображения, но это суть решения. Возможно, стоит включить это в пользовательский TextView
.