正如我的标题所描述的那样,我希望TextView中的文本在其父元素中水平居中,同时保持文本左对齐:
所以这个:
| Text |
| TextText |
成为这个:
| Text |
| TextText |
这可以用于Android吗?
这是我目前的布局:
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="@drawable/table_cell"
android:gravity="center"
android:text="@string/table_last_training" />
提前致谢, 麦克法兰
答案 0 :(得分:0)
此布局的更新答案。
|文字| | TextText |
我将layout_height更改为wrap_content。还添加了TableRow。 我个人不喜欢TableLayout,因为它不可预测和麻烦,特别是在使用合并列colspan时。 我更喜欢对每一行使用LinearLayout和layout_weight。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/table_shape" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/cell_shape3"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.375" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Text" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.375" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/cell_shape3"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.375" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Text Text" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.375" />
</LinearLayout>
</TableRow>
</TableLayout>
</ScrollView>
答案 1 :(得分:0)
private String hackSpace(Context context, float textSize, String text) {
TextView textView = new TextView(context);
textView.setTextSize(textSize);
ViewGroup.LayoutParams textViewParams = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
textView.setLayoutParams(textViewParams);
String[] sentences = text.split("\n");
//calculate space width
textView.setText(" ");
textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int slice = textView.getMeasuredWidth();
//calculate each sentence
int[] sentencesWidth = new int[sentences.length];
//store max width
int maxWidth = 0;
for (int i = 0; i < sentences.length; i++) {
textView.setText(sentences[i]);
textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
sentencesWidth[i] = textView.getMeasuredWidth();
if (sentencesWidth[i] > maxWidth) {
maxWidth = sentencesWidth[i];
}
}
//add space after each sentence if necessary
for (int i = 0; i < sentences.length; i++) {
int spaceNum = (maxWidth - sentencesWidth[i]) / slice;
StringBuilder sb = new StringBuilder();
for (int k = 0; k < spaceNum; k++) {
sb.append(" ");
}
sentences[i] += sb.toString();
}
//rebuild String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sentences.length; i++) {
sb.append(sentences[i]);
if (i != sentences.length - 1) {
sb.append("\n");
}
}
return sb.toString();
}