Reputation: 13
How is the best way to set gravity for TextView and ImageButton? I would like have these in horizontal position and TextView should take 90% of the space and ImageButton 10%.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/info"/>
</LinearLayout>
Upvotes: 1
Views: 27
Reputation: 4425
Look at another option which can be possible now with a constraint layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:padding="16dp">
<Button
android:id="@+id/btnThird"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="Hello"
app:layout_constraintHorizontal_weight=".8"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btnTwoThirds" />
<Button
android:id="@+id/btnTwoThirds"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Wordl"
app:layout_constraintBottom_toBottomOf="@+id/btnThird"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintLeft_toRightOf="@+id/btnThird"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
Upvotes: 0
Reputation: 8282
Try this..Use android:layout_weight=".3"
make you textview
width is 0dp like this android:layout_width="0dp"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".3"
android:text="TextView" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="0dp"
android:layout_weight=".1"
android:layout_height="match_parent"
android:src="@android:drawable/btn_dropdown"/>
</LinearLayout>
Upvotes: 0
Reputation: 4678
You can use layout properties as wieghtSum and layout weight. You can use below code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_wightSum="1" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight=".9" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/info"
android:layout_weight=".1"
/>
I hope that solves your problem..
Upvotes: 1