如何将此textView对齐到垂直居中?

时间:2014-01-30 15:30:49

标签: android xml textview vertical-alignment

好的,我有这个问题:enter image description here

我疯了,因为我现在已经试着找到这个问题的答案大约3个小时了。 无论如何,我想将第一个textView(如图所示)对齐到垂直居中。 我以编程方式创建这些textViews(不需要问为什么,我只需要)。 代码:

private View formTextViewUnits(String units) {
    TextView textViewUnits = new TextView(getActivity());
    textViewUnits.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    textViewUnits.setText(units);
    textViewUnits.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
    textViewUnits.setTextColor(Color.parseColor("#e9e9e9"));
    textViewUnits.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getResources().getDimension(R.dimen.main_stripe_small_text));

    return textViewUnits;
}

private View formTextViewAmount(Float amount) {
    TextView textViewAmount = new TextView(getActivity());
    textViewAmount.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    textViewAmount.setText(amount.toString());
    textViewAmount.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
    textViewAmount.setTextColor(Color.parseColor("#3ebce6"));
    textViewAmount.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getResources().getDimension(R.dimen.main_stripe_big_text));

    return textViewAmount;
}

//THIS IS THE FAULTY TEXTVIEW
private View formTextViewMessage(String message) {
    TextView textViewMessage = new TextView(getActivity());
    textViewMessage.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    textViewMessage.setText(message);
    textViewMessage.setGravity(Gravity.CENTER_VERTICAL);
    textViewMessage.setTextColor(Color.parseColor("#e9e9e9"));
    textViewMessage.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getResources().getDimension(R.dimen.main_stripe_small_text));

    return textViewMessage;
}

这是我的片段表行(使用tableLayout):

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/main_stripe_padding_top"
    android:paddingBottom="@dimen/main_stripe_padding_bottom"
    android:layout_marginBottom="@dimen/main_stripe_margin_bottom"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:background="#424242">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_span="2"
        android:gravity="right"
        android:paddingLeft="@dimen/main_stripe_text_padding_left"
        android:paddingRight="@dimen/main_stripe_text_padding_right"
        android:orientation="horizontal"
        android:id="@+id/linearLayoutStripe">
    </LinearLayout>

    </TableRow>

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

TextView.setGravity(...)TextView 的范围内设置文本的重力。由于有问题的TextView的垂直高度为wrap_content,因此视图的边界始终是内容的高度。

如果您将TextView的高度设置为match_parent,并保持现在的重力设置,则TextView中的文字应在其父版式中垂直居中显示。