不知何故,我无法将 CustomTextView 小部件文本居中,这应该是一行代码。
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(0, height, 1);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);
masterLinearLayout.addView(linearLayout);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textViewParams.gravity = Gravity.CENTER_HORIZONTAL;
CustomFontTextView valueTextView = new CustomFontTextView(this);
valueTextView.setLayoutParams(textViewParams);
valueTextView.setTextAppearance(this, R.style.CoolStyle);
valueTextView.selectCustomFont(getString(R.string.default_regular_font));
linearLayout.addView(valueTextView);
还有我的风格“CoolStyle”:
<style name="CoolStyle" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">@dimen/..</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_margin">3dip</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:singleLine">true</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
奇怪的是 - textColoring 和 textSize 确实有效,但我似乎无法将文本居中并使其成为单个文本行。我究竟做错了什么?