[Solved] I had to add android:fillViewport="true"
to the ScrollView, that fixed the problem with the text not centering vertically.
I know this has been answered many times before, but I´m still not able to center a textview´s text vertically.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="@dimen/icon_width"
android:layout_height="@dimen/icon_height"
android:src="@drawable/picture" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/icon"
android:gravity="center_vertical"
android:layout_alignBottom="@+id/icon"
android:layout_alignTop="@+id/icon"
android:text="@string/title_text"
android:textSize="@dimen/textsize"
android:textStyle="bold"
android:textColor="@color/color"
android:shadowColor="@color/shadow"
android:shadowRadius="5"/>
</RelativeLayout>
</ScrollView>
Normally this should work with
android:gravity="center_vertical"
but it has no effect on the textview...
Weird thing is that I have a second app with the exact same code and it´s working there without any problems.
/edit
To clarify my question:
This is what I have right now:

This is what I want:

replace your TextView with this code and it will work fine
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/icon"
android:layout_alignParentTop="true"
android:layout_alignTop="@+id/icon"
android:layout_toRightOf="@+id/icon"
android:shadowColor="@color/shadow"
android:shadowRadius="5"
android:text="@string/title_text"
android:textColor="@color/color"
android:textSize="@dimen/textsize"
android:textStyle="bold" />
To achieve the result you want, it will be enough to remove
android:gravity="center_vertical"
android:layout_alignBottom="@+id/icon"
android:layout_alignTop="@+id/icon"
and add
android:layout_centerVertical="true"
instead.
android:gravity specifies how a parent will position its children. In this case the parent is the TextView and the child is the actual text within that view. If the TextView (parent) is not wider than the text (child) then gravity will have no effect.
The android:gravity docs for reference:
Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view.
Set the width of your TextView like this:
android:layout_width="match_parent"
That should cause the Text to get centered vertically.
When your are using Relative Layout, you can add to your TextView:
`android:layout_centerVertical="true"`
One solution that DOES work (all the above don't) if you are only concerned about your text itself, is to make the textView just as high as the imageView (by aligning it to top without margin, and bottom to id of "other views") and then use android:gravity="center" in this text box (make sure no other alignments interfere with this gravity)
I think you can try one of these ways:
- in TextView ,set android:layout_centerVertical="true"
or
- as the 1st answer, but I think it should be android:layout_height="match_parent"
why your code in second app works, I guess , maybe the height of image equals the textView
for others that have this problem there is a bug in Relative Layouts after api 18 see https://code.google.com/p/android/issues/detail?id=59368
a fix that worked for me was to replace my relative layout with this code (found in the above link)
DISCLAIMER : this only worked in one specific situation. It failed horrible in another. Feel free to try it out if you want
public class FixRelativeLayoutBug extends RelativeLayout {
public FixRelativeLayoutBug(Context context) {
super(context);
}
public FixRelativeLayoutBug(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FixRelativeLayoutBug(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private static final int AT_MOST_UNSPECIFIED = MeasureSpec.makeMeasureSpec((1<<30)-1, MeasureSpec.AT_MOST);
@Override
protected void onMeasure(int widthSpec, int heightSpec)
{
// RelativeLayout has bugs when measured in UNSPECIFIED height mode that were supposedly fixed
// in API 18. However the 'fix' does not work with breaks gravity == center_vertical in TextView.
// https://code.google.com/p/android/issues/detail?id=63673
if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED) heightSpec = AT_MOST_UNSPECIFIED;
super.onMeasure(widthSpec, heightSpec);
}
}
来源:https://stackoverflow.com/questions/18724144/can%c2%b4t-center-textview%c2%b4s-text-vertically