3

I have a textview in a horizontal linear layout, and the textview is being top aligned. I want it to be vertically aligned.

I have the textview set to a style called "arrow"

<style name="arrow">
    <item name="android:textSize">30sp</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:textStyle">bold</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:layout_height">wrap_content</item> 
</style>

java

            LinearLayout LL = new LinearLayout(this);
            LL.setOrientation(LinearLayout.HORIZONTAL);
            LL.setPadding(15, 15, 15, 15);
            LinearLayout.LayoutParams courseMargin = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            courseMargin.setMargins(5, 5, 5, 5);
            LL.setLayoutParams(courseMargin);

                            // here I add more things into LL...

            TextView arrow = new TextView(this);
            arrow.setText(">");
            arrow.setTextAppearance(this, R.style.arrow);
            LL.addView(arrow);

However this is still not making it vertically aligned...

Does anyone know whats going here?

Thanks

omega
  • 40,311
  • 81
  • 251
  • 474

2 Answers2

4

The javadocs for setTextAppearance() state:

Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.

Which doesn't include anything about position or gravity. I would guess that is why your gravity is not having any effect.

Try like this:

//for android:gravity
arrow.setGravity(Gravity.CENTER);

//for android:layout_gravity
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER;
arrow.setLayoutParams(params);

That being said, since your TextView is width and height WRAP_CONTENT, android:gravity is not going to have any effect. So really the android:layout_gravity chunk of code should be all that you need.

Cat
  • 66,919
  • 24
  • 133
  • 141
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
0

You can also try using SetGravity on your textView. setGravity

prijupaul
  • 2,076
  • 2
  • 15
  • 17