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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With