0

Somehow I've trouble centering CustomTextView widgets text, which should be one code of line.

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);

And my syle "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>

What is weird - textColoring and textSize actually works, but I cannot seem to center text and make it single text line. What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
MaaAn13
  • 264
  • 5
  • 24
  • 54

1 Answers1

0

Firstly, check your parent LinearLayout, whether it has full width or not. Secondly, you are setting layout_width textView as WRAP_CONTENT programmatically, but style has opposite MATCH_PARENT. Then, you are setting textAppearance for textViewParams, it won't affect anything. Try to set it for layout and give proper gravity.

qwerty qwerty
  • 26
  • 1
  • 3