2014-10-22 35 views
-2

在我的应用程序中,我已经显示了两个按钮。其中的文字如下所示。我怎样才能从同一行(中心)对齐两个文本。如何将中心的按钮文字对齐

因此很显然,我要开始按钮文本 “Amiyo” & “Amiyo比斯瓦斯” 从同一行(中心)

这里是我的XML:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight=".75" 
    android:orientation="vertical" 
    android:layout_marginTop="15dp" 

    > 



    <Button   
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".10" 
     android:text="Amiyo"   
     android:gravity="center" 
     android:textAlignment="gravity" 
     /> 

    <Button   
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".10" 
     android:text="Amiyo Biswas"   
     android:gravity="center_horizontal|center_vertical" 
     android:textAlignment="gravity" 
     /> 

    </LinearLayout> 

enter image description here

我想like this alignment enter image description here

+1

他们在中心。由于文本长度有所不同。 – 2014-10-22 09:34:09

+0

是的。我明白 。有什么办法可以解决这个问题吗? – 2014-10-22 09:34:53

+4

解决什么问题?没有什么可解决的。 – 2014-10-22 09:38:04

回答

0

试试这个

首先设置文本对齐到左| center_vertical,然后设置文本的左侧填充

<LinearLayout 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" 
android:orientation="vertical" 
tools:context=".MainActivity" 
tools:ignore="HardcodedText" > 

<Button 
    android:id="@+id/left" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="left|center_vertical" 
    android:paddingLeft="50dp" 
    android:text="Button 1" /> 

    <Button 
    android:id="@+id/left" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="left|center_vertical" 
    android:paddingLeft="50dp" 
    android:text="Button 2" /> 

</LinearLayout> 
0

尝试如下代码this..I试图和这个工程(在同一条线上两个按钮也居中)

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginLeft="50dp" 
    android:text="Amiyo" /> 

    <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="Amiyo Biswas" /> 

</LinearLayout> 

复制此XML文件中......

尝试使用列表视图: - 参考以下链接: - (http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90

 <ListView 
      android:id="@+id/list" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"> 
    </ListView> 

</LinearLayout> 
+0

感谢您的关注。我使用android:layout_width =“fill_parent”而不是wrap_content – 2014-10-22 10:00:15

+1

在我看来,你可以使用Listview,你可以参考这个链接:-http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view = article_discription&aid = 65&aaid = 90 – user3173628 2014-10-22 11:24:32

2

如果我正确理解你,你希望按钮文本相互对齐。以下仅通过垂直居中进行。水平对齐是在左边,我添加了一些填充,所以它仍然看起来有点水平居中。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight=".75" 
    android:orientation="vertical" 
    android:layout_marginTop="15dp" 
    > 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".10" 
     android:text="Amiyo" 
     android:gravity="center_vertical" 
     android:paddingLeft="120dp" 
     /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".10" 
     android:text="Amiyo Biswas" 
     android:gravity="center_vertical" 
     android:paddingLeft="120dp" 
     /> 

</LinearLayout> 

Screenshot

注意,手动设置填充,使它看起来水平居中只能在一个屏幕尺寸的工作。如果你在不同的设备上使用不同的屏幕宽度运行它,它会再次出现错误。为了解决这个问题,你必须在代码中进行一些计算并以编程方式设置填充。但是在那个时候,我会放弃,只是把它左对齐。或者开始了解自己编写小部件并正确解决这个问题,但这需要一些时间。

0

试试这个方法,希望这会帮助你解决你的问题。

在这里,我玩重量,所以可能会适合您的要求。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="1dp" 
     android:layout_weight="0.35"/> 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.65" 
     android:orientation="vertical"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="ONE MILE"/> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIVE MILES"/> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TEN MILES"/> 
    </LinearLayout> 
</LinearLayout> 
-1

此代码是硬编码的,你可以试试这个:

<LinearLayout 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" 
android:orientation="vertical"> 


<Button 
     android:id="@+id/button1"   
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="87dp" 
     android:text="Amiyo" />   

<Button   
    android:id="@+id/button2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 

    android:paddingLeft="105dp" 
    android:text="Amiyo Biswas" />  

</LinearLayout> 
+1

我试图避免硬编码值。可能我需要定制课程 – 2014-10-22 10:28:30