如下图所示,我希望每个button
的文本都以中心为中心,而不是在这个意义上说,文本应该从中心开始写,但从文本的中心应该开始也是视图的中心。如何使子视图文本的中心位于其父视图的中心?
我在xml文件中使用了gravity
属性,layout_centerInParent="true"
也是如此,但正如您在下面看到的那样,文本开始从视图的中心写入。
请让我知道哪个属性可以完成这项工作?
XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/et_IP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="IP Address" />
<EditText
android:layout_below="@+id/et_IP"
android:id="@+id/et_port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="1883" />
<Button
android:layout_below="@+id/et_port"
android:id="@+id/bt_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Connect..."/>
<Button
android:layout_below="@+id/bt_connect"
android:id="@+id/bt_clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Clear"/>
<TextView
android:layout_below="@+id/bt_clear"
android:gravity="center"
android:id="@+id/tv_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO_RESPONSE"/>
</RelativeLayout>
产品图
我认为你需要一个customview来做到这一点 – TheRedFox 2014-11-06 14:11:29