Reputation: 73
I want to put my image (id:imgAbout) at the bottom of the page.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9AB89C"
android:orientation="vertical"
tools:context=".Vue.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvDevice"
android:layout_width="match_parent"
android:layout_height="400sp"
android:layout_marginTop="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:onClick="aboutPage"
android:id="@+id/imgAbout"
android:layout_marginTop="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_about"
android:background="#9AB89C"
android:layout_gravity="bottom"
android:paddingBottom="0dp"/>
</LinearLayout>
I do not see how to do that in a LinearLayout.should I use a RelativeLayout?
Upvotes: 0
Views: 44
Reputation: 371
-First of all never use static height or width for a Component (RecyclerView). Use wrap content or match parent.. -Second if you will use static height or width, using dp is a better choice than sp (sp is preferable for textSize)..
If you prefer LinearLayout you can use this one. (%90 percentage of screen will be RecyclerView and %10 percentage of screen will be Button, vertically)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9AB89C"
android:orientation="vertical"
android:weightSum="10">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvDevice"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9" />
<ImageButton
android:id="@+id/imgAbout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#9AB89C"
android:onClick="aboutPage" />
</LinearLayout>
And if you prefer RelativeLayout use this one:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9AB89C">
<ListView
android:id="@+id/rvDevice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="@+id/imgAbout"
android:layout_marginTop="20sp" />
<ImageButton
android:id="@+id/imgAbout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#9AB89C"
android:layout_alignParentBottom="true"
android:onClick="aboutPage" />
</RelativeLayout>
Both will do what you want..
Upvotes: 2
Reputation: 16547
Use a RelativeLayout something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9AB89C"
tools:context=".Vue.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvDevice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp" />
<ImageButton
android:onClick="aboutPage"
android:id="@+id/imgAbout"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_about"
android:background="#9AB89C"
android:layout_gravity="bottom"
android:paddingBottom="0dp"/>
</RelativeLayout>
FYI height margin and padding should be assigned in dp where as text size should be in sp
Upvotes: 1
Reputation: 2564
Set recycler view's width and height to this
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
Upvotes: 3