How can I make some layout to be for example 80% or 90% of the width of the screen, no matter what is the screen size or DPI? I tried with " but I get 80% of the height then.
EDIT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background" >
<Button
android:id="@+id/bNivoi1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:background="@drawable/button_nivoi_back" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvOpis15Nivoa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="15sp"
android:layout_weight="0.8"
android:text="text" />
</LinearLayout>
</LinearLayout>
I only need textview to be 80% of the screen, not the rest of the activity.
If you wanted to get the actual width of your view you would have to call getWidth() on your view once the layout has been inflated and displayed. Calling getWidth() before your layout has been displayed will result in 0. Show activity on this post. Which onMeasure() call are you talking about?
android:weightSum. Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.
Layout Weight This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.
android:layout_weight
is based on the android:orientation
of your LinearLayout
- if you want your layout to take up 80% of the width, you need a LinearLayout
with an android:orientation="horizontal"
<LinearLayout
android:id="@+id/your_outer_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <!-- note that vertical is the default -->
<!-- Other elements here -->
<LinearLayout
android:id="@+id/eighty_percent_layout_holder
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<View
android:id="@+id/your_eighty_percent_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8" />
</LinearLayout>
<!-- Other elements here -->
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With