2016-08-11 62 views
0

我的問題是我在屏幕上有四個按鈕,我使用PercentageRelativeLayout將它們放置在我想要的位置,並且一切都很好。
他們甚至調整大小!
但是,當他們調整大小時,按鈕開始相互接觸,當我使它們在平板電腦上互不接觸時,當我回到手機時,空間相隔太遠。使用百分比相對佈局創建間距

有沒有人有任何想法如何創建一個可以在所有設備上工作的間距?

因爲我認爲PercentageRelativeLayouts是讓事情調整大小的完美解決方案,而不使用任何LinearLayout,並且它沒有按照我的想法工作。

下面是我的代碼

<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" 
android:background="#696969"> 

<ImageButton 
    app:layout_widthPercent="40%" 
    app:layout_heightPercent="40%" 
    app:layout_marginLeftPercent="9%" 
    app:layout_marginTopPercent="21%" 
    android:background="#00ff0000" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/opt1" 
    android:scaleType="fitCenter" 
    android:id="@+id/purpleButton" 
    android:layout_align="@+id/option1" 
    android:longClickable="false" /> 

<ImageButton 
    app:layout_widthPercent="40%" 
    app:layout_heightPercent="40%" 
    app:layout_marginLeftPercent="9%" 
    app:layout_marginTopPercent="58%" 
    android:background="#00ff0000" 
    android:layout_height="fill_parent" 
    android:src="@drawable/opt2" 
    android:scaleType="fitStart" 
    android:id="@+id/option2" 
    android:layout_width="fill_parent" 
    android:longClickable="false" /> 
<ImageButton 
    app:layout_widthPercent="40%" 
    app:layout_heightPercent="40%" 
    app:layout_marginLeftPercent="50%" 
    app:layout_marginTopPercent="58%" 
    android:background="#00ff0000" 
    android:layout_height="fill_parent" 
    android:src="@drawable/opt3" 
    android:scaleType="fitStart" 
    android:id="@+id/option3" 
    android:layout_width="fill_parent" 
    android:longClickable="false" /> 
<ImageButton 
    app:layout_widthPercent="40%" 
    app:layout_heightPercent="40%" 
    app:layout_marginLeftPercent="50%" 
    app:layout_marginTopPercent="21%" 
    android:background="#00ff0000" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/option4" 
    android:scaleType="fitCenter" 
    android:id="@+id/opt4" 
    android:longClickable="false" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Pick an Answer" 
    android:id="@+id/pickLabel" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="28dp" 
    app:layout_widthPercent="100%" 
    app:layout_heightPercent="7%" 
    app:layout_marginTopPercent="15%" 
    android:textColor="#ffffff" 
    android:textAlignment="center" 
    android:textSize="30sp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:layout_widthPercent="100%" 
    app:layout_heightPercent="12%" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="00" 
    android:id="@+id/timeLabel" 
    android:textAlignment="gravity" 
    android:textSize="72sp" 
    android:gravity="center|center_vertical|center_horizontal" /> 

+1

發佈您當前的佈局。 – FlyingPumba

+0

@FlyingPumba剛剛添加了佈局 –

+0

@Rotwang當我使用dps時,視圖永遠不會調整大小,無論屏幕大小如何,它始終保持相同的大小 –

回答

0

可以對內部佈局外佈局和android:layout_weight="0.25"設置android:weightSum="1"

+0

僅權重屬於LinearLayouts ** ** –