当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Create Spacing with Percentage Relative Layout

问题描述:

My problem is that I have four Buttons on the screen, I used a PercentageRelativeLayout to place them where I want them and everything was working great.

They even resize!

However, when they do resize, the Buttons start touching each other, and when I make it so they do not touch each other on a tablet, when I go back to a phone the space is too far apart.

Does anyone have any idea on how I could create a spacing that could work on all devices?

Because I thought that PercentageRelativeLayouts were the perfect solution to get things to resize, without using any LinearLayout and it is not working out the way I thought.

Below is my code

<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" />

网友答案:

You can set android:weightSum="1" on the outside layout and android:layout_weight="0.25" for the inside layouts.

分享给朋友:
您可能感兴趣的文章:
随机阅读: