Reputation: 2615
Hi i have an app that is a gallery of images.
The problem is that, i have buttons to move between images on the center of the app and i want them at 75% of the margin of top.
The question is : Can i put it on % of the screen instead of dp?
The error is that if i use margin_bottom
it crashes.
Here is my code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/color_fondo"
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=".MainActivity" >
<ImageView
android:id="@+id/barra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"a
android:src="@drawable/cabecera" />
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/barra"
android:paddingTop="@dimen/activity_vertical_margin"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<ProgressBar
android:id="@+id/pb1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/img1"
android:layout_centerHorizontal="true"
android:visibility="invisible" />
<ImageButton
android:id="@+id/imgbtnshare1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@null"
android:paddingBottom="5dp"
android:src="@drawable/compartir4" />
<ImageButton
android:id="@+id/imgbtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imgbtnshare1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="100dp"
android:background="@null"
android:src="@drawable/izquierda42" />
<ImageButton
android:id="@+id/imgbtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imgbtn2"
android:background="@null"
android:src="@drawable/derecha42" />
</RelativeLayout>
Upvotes: 2
Views: 419
Reputation: 16450
You may get the Layout
size and then calculate your buttons top margin as proportion of that:
NOTE: You cannot get the layout size in onCreate()
since it's not set yet. Do it in onWindowFocusChanged
method of your Activity
.
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.YourXML);
@Override
public void onWindowFocusChanged (boolean hasFocus) {
// The layout has been drawn here
// so the Height and Width have been computed
int width = relativeLayout.getWidth();
int height = relativeLayout.getHeight();
}
int topMargin = height * 0.75;
And set it programmatically:
LayoutParams layoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
layoutParams.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(layoutParams);
Upvotes: 2