2

正如我的标题所描述的,我希望 TextView 中的文本在其父元素中水平居中,同时保持文本左对齐:

所以这:

|     Text     |
|   TextText   |

变成这样:

|   Text       |
|   TextText   |

安卓可以吗?

这是我目前的布局:

<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" />

在此先感谢,麦克法兰

4

2 回答 2

0

更新了此布局的答案。

| 正文 | | 文字文字 |

我将 layout_height 更改为 wrap_content。还添加在 TableRow 内。我个人不喜欢 TableLayout,因为它不可预测且麻烦,尤其是在使用合并列 colspan 时。我更喜欢对每一行使用带有 layout_weight 的 LinearLayout。

 <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>
于 2013-08-21T10:43:16.473 回答
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();
}
于 2017-05-09T08:23:26.123 回答