问题
Even though I made gravity center text is still on left. I assume there must be a method to change text align (I have tried android:textAlignment="center" too)
I've included screenshot below but dont have enough reputation to make show it.
<!-- language: lang-xml-->
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:text="Button" />
also tried to make it match_parent still the same problem.
EDIT for whole XML
<!-- language: lang-xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etCommands"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type a Command"
android:password="true" >
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Button" />
</LinearLayout>
<TextView
android:id="@+id/tvResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="invalid" />
</LinearLayout>
ScreenShot
回答1:
I had this problem too. Just restart your IDE.
回答2:
Adding android:layout_gravity="center" to the layout for the button and have the width wrap_content should fix it.
来源:https://stackoverflow.com/questions/13660415/center-aligned-buttons-text-on-left