2016-08-03 61 views
1

我有一個佈局,其中只包含一個帶有3個按鈕的LinearLayour。方形按鈕屏幕寬度和重量匹配

我希望這3個按鈕垂直填充屏幕,所以我使用「weigth = 1」,但我也應該想要按鈕是方形的,也就是說,它們的寬度等於它們的高度。

任何方式來做到這一點?

這裏的.xml文件

<?xml version="1.0" encoding="utf-8"?> 

<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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="android.jose.se.Prueba" 
    android:background="@drawable/bg" 
    android:gravity="center"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="vertical"> 

    <Button 
     android:id="@+id/bHet" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@drawable/layer1" 
     android:onClick="cambiarBoton"/> 

    <Button 
     android:id="@+id/bGay" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@drawable/layer2" 
     android:onClick="cambiarBoton"/> 

    <Button 
     android:id="@+id/bLesbi" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@drawable/layer3" 
     android:onClick="cambiarBoton"/> 

</LinearLayout> 

謝謝大家

+0

你想垂直或水平填充? – faranjit

+0

這可能會有所幫助:http://stackoverflow.com/questions/4656986/how-do-i-keep-the-aspect-ratio-on-image-buttons-in-android – Bill

+0

縱向上,我已經糾正它。 – JOSEMAFUEN

回答

0

做到這一點的最好辦法,就是讓一個名爲Square按鍵新的和這個類應該擴展按鈕。然後在這個類的onMeasure中,只需將widthSpec設置爲WidthSpec。

public class SquareButton extends Button { 

public SquareButton(Context context) { 
    super(context); 
} 

public SquareButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public SquareButton(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@Override 
protected void onMeasure(int WidthSpec, int HeightSpec){ 
    int newWidthSpec = HeightSpec ; 

    super.onMeasure(newWidthSpec, HeightSpec); 
}} 

現在你可以在你的xml中使用這個SquareButton而不是普通的Button了。 android:width屬性的值將被忽略。

<com.example.package.SquareButton 
    android:height = "0dp" 
    android:weight = "1" 
    android:width = "0dp" 
/> 
2

我有類似的問題,並使用Google's Percent Relative Layout,可以幫助您管理視圖的寬高比做到了。

您可以使用它像這樣

<android.support.percent.PercentRelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <Button 
     android:layout_width="match_parent" 
     app:layout_aspectRatio="100%"/> 
</android.support.percent.PercentFrameLayout> 
+0

我試過了,但它說「佈局高度屬性應該定義」 – JOSEMAFUEN

+0

忽略該錯誤,它會起作用。只是建立和嘗試。這是我認爲的一個缺陷。 –

+0

並注意「layout_ *」屬性僅用於佈局。所以佈局可能根本不需要「layout_height」,就像在這種情況下一樣。 –