2014-08-28 62 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