2014-11-06 83 views
1

如下图所示,我希望每个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> 

产品图 enter image description here

+0

我认为你需要一个customview来做到这一点 – TheRedFox 2014-11-06 14:11:29

回答

0

我最近做了这样的事情在页面上居中,并在视图按钮。这是一个很有用的小例子。

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" > 
<LinearLayout> 
<!-- Add other controls that you dont want with buttons --></LinearLayout> 
<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:paddingBottom="10dip" 
     android:paddingLeft="10dip" 
     android:paddingRight="10dip" 
     android:paddingTop="10dip" 
     android:layout_marginBottom="15dip" 
     android:layout_gravity="bottom|center" 
     android:layout_below="@id/login_form" > 

     <Button 
      android:id="@+id/edit_button" 
      style="@style/Button.Green" 
      android:layout_width="133dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_marginTop="10dip" 
      android:text="@string/OK" /> 

     <Button 
      android:id="@+id/unlock_button" 
      style="@style/Button.Green" 
      android:layout_width="136dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_marginTop="10dip" 
      android:text="@string/unlock" /> 

      <Button 
      android:id="@+id/reset_password_button" 
      style="@style/Button.Green" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dip" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:text="@string/force_reset_password" /> 
    </LinearLayout> 
</merge> 

如果您在层次结构中工作,合并是实现视图/布局的好工具。希望它有帮助:)

+0

嗨,我试图没有指定任何边距,它没有工作?合并的用途是什么? – user2121 2014-11-06 15:18:30

+0

标记有助于在将一个布局包含在另一个布局中时消除视图层次结构中的冗余视图组。例如,如果您的主布局是一个垂直LinearLayout,其中两个连续视图可以在多个布局中重新使用,那么放置两个视图的可重用布局需要它自己的根视图。但是,使用另一个LinearLayout作为可重用布局的根将导致垂直LinearLayout内的垂直LinearLayout。除了减慢UI性能之外,嵌套的LinearLayout没有真正的用途。 – Chetandalal 2014-11-06 15:35:58

相关问题