我有一个带有标题TextView的LinearLayout,然后是一个包含TextView的ListView。这与其中有一个GridView的另一个类似的LinearLayout相邻。带有TextView的ListView只能对文本进行中心打包
ListView中的TextView不会居中,除非文字由于很长而回绕。
当我硬连线ListView的宽度时,我确实将它们居中,但是它有另一个副作用,即长条线两端的文本被切断,即使它被包裹。只是没有正确包装。所以我通过将硬编码宽度移动到封闭的LinearLayout来解决这个问题,但现在有这个问题。
有趣的是,我的类似的GridView似乎没有问题,虽然它也有一个'背景可绘制',这是一个带圆角的矩形。
布局是:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:id="@+id/course_part"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/course"
android:textSize="@dimen/text_course_title"
android:textColor="#ff000000"
android:textStyle="bold" />
<ListView
android:id="@+id/listview_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
并用于填充的ListView TextView的是:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_mark_name_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fontFamily="Roboto"
android:textAlignment="center"
android:text="name"
android:textColor="#ff000000"
android:textSize="@dimen/text_course_grid"
android:textStyle="bold"
android:typeface="monospace"
/>
而且我要指出,我基于动态改变字体大小在我ArrayAdapter“getView”文本的长度:
// First shrink it's size to better fit.
float textSize = 30; // In DP units.
int nameLength = mark.name.length();
if (nameLength > 4)
{
if (nameLength <= 8)
textSize *= 4.0f/nameLength; // Up to 8, proportionally shrink it.
else
textSize *= 0.5f; // After 8, leave it at half size.
}
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
holder.textView.setMinHeight(this.textViewPixels);
holder.textView.setText(mark.name); // Set it's name.
holder.textView.setTextColor(appData.getNightMode() ? Color.WHITE : Color.BLACK); // Just added it. Set it's color properly.
我已经尝试过gravity =“center”和textAlignme NT =“中心”,他们都表现相同。
我已经尝试过各种包含视图/布局的match_parent和fill_parent的各种组合,通常效果不好,而且如果我不强制宽度,我会得到太多空间或者非常薄的无用列。
谢谢!
尝试android:gravity =“center_horizontal” –
试过了。没有快乐。 –