2014-08-28 61 views
0

现在,我的登录按钮比我的应用程序名称文本更宽,因为它使用固定的相对布局填充,但我希望它自动扩展到行与应用程序名称文本的左边缘和右边缘对齐。有没有什么办法以编程方式做到这一点?使按钮宽度等于Android上的不同文本宽度(不是按钮的文本)Android

我希望它在所有设备上看起来都一样。我担心,如果我只是硬编码自己的页边空白,它会在我的设备上看起来很正确,但如果它们呈现不同的字体,可能无法在其他设备上正确显示。

谢谢!

layout.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" 
android:animateLayoutChanges="true" 
android:background="@drawable/back_image" 
android:padding="20dp" > 

    <TextView 
    android:id="@+id/AppNameTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="@dimen/margin_top" 
    android:fontFamily="Arial" 
    android:text="@string/app_title" 
    android:textColor="@color/white" 
    android:textSize="@dimen/text_size" /> 

    <com.timey.widget.Button 
    android:id="@+id/signInButton" 
    android:layout_width="match_parent" 
    android:layout_height="65dp" 
    android:background="@color/green" 
    android:fontFamily="Arial" 
    android:text="@string/sign_in" 
    android:textColor="@color/white" 
    android:textSize="30sp" /> 

</RelativeLayout> 

样机用红色虚线说明我多么希望应用程序名称的两侧的我的登录按钮两侧排队:

enter image description here

+0

尝试使用线性布局 – Nabin 2014-08-28 01:46:32

+0

这将如何使它们具有相同的宽度? – 2014-08-28 01:48:38

+0

等待给你答案 – Nabin 2014-08-28 01:49:38

回答

0

我不完全确定这是否有帮助,但让我试试看。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="App\nTitle" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@id/title" 
     android:layout_alignRight="@id/title" 
     android:layout_below="@id/title" 
     android:text="Log In" /> 

</RelativeLayout> 

但是,按钮会和textview一样宽,它可能不足以显示它自己的文本。

+0

谨慎解释downvote?它可以正常工作,当我在我的机器上测试它时 – CChi 2014-08-28 01:58:35

+0

是的,它完美的工作正常,@CCHI。从我+1。虽然它与我的相似;-)。为什么要投票? – Nabin 2014-08-28 02:04:22

+0

结果可能相似,但我认为相关布局是一项要求。 – CChi 2014-08-28 02:06:07

0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Button2" /> 

</LinearLayout> 

第一个按钮将占用尽可能多的空间。然后,下一个按钮(或者你的情况)会占用与其父母(线性布局)一样多的空间,而在另一侧将会占用更多的空间,即第一个按钮的宽度。所以这两个按钮将采用相同的宽度。您可以将其用于任何其他组件。

UPDATE1

Key to the success 
1. A container with wrap_content 
2. First component should have wrap_content 
3. Second component should have match_content 

UPDATE2 为RelativeLayout的使用layout_alignLeft = “@ + ID /按钮1” 和layout_alignRight = “@ + ID/Button1的” 这将调整到左和右第一个组件即按钮1在这里。

+0

虽然我没有使用2个按钮。我正在尝试排列文字和按钮。 – 2014-08-28 01:56:21

+0

这个概念对上述答案中提到的任何组件都可以很好地使用 – Nabin 2014-08-28 01:56:55

+0

我需要使用RelativeLayout而不是LinearLayout。 – 2014-08-28 01:59:52