Android Percentage Layout Height
I know that it is impossible to set percentages and that you can set a weight of certain images to scale their heights. What I am trying to do though is specify the height of a layout relative to the layout it is within. Basicly I have something like this
<LinearLayout android:layout_height="fill_parent">
<LinearLayout>
</LinearLayout>
</LinearLayout>
Of course this is a very simplified version, just so you can understand my gibberish. Basicly I want to set the inner line开发者_JAVA技巧arlayout to be around 50% of the main linearlayout.
What is the best way to do this?
There is an attribute called android:weightSum.
You can set android:weightSum="2" in the parent linear_layout and android:weight="1" in the inner linear_layout.
Remember to set the inner linear_layout to fill_parent so weight attribute can work as expected.
Btw, I don't think its necesary to add a second view, altough I haven't tried. :)
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:weightSum="2">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
You could add another empty layout below that one and set them both to have the same layout weight. They should get 50% of the space each.
android:layout_weight=".YOURVALUE" is best way to implement in percentage
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/logTextBox"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".20"
android:maxLines="500"
android:scrollbars="vertical"
android:singleLine="false"
android:text="@string/logText" >
</TextView>
</LinearLayout>
Just as you said, I'd recommend weights. Percentages would be incredibly useful (don't know why they aren't supported), but one way you could do it is like so:
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="1"
>
</LinearLayout>
<View
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
The takeaway being that you have an empty View that will take up the remaining space. Not ideal, but it does what you're looking for.
With introduction of ContraintLayout, it's possible to implement with Guidelines:
<?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"
tools:context="com.example.eugene.test1.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#AAA"
android:text="TextView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/guideline" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
</android.support.constraint.ConstraintLayout>
You can read more in this article Building interfaces with ConstraintLayout.
You can add Constraint Layout 1.1 using this line in your android projects:
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
In Constraint Layout 1.1 it’s been made simpler by allowing you to easily constrain any view to a percentage width or height.
If you want to set the height of the linearlayout to be around 50% of the main linearlayout , you can do it like this:
<LinearLayout android:layout_height="fill_parent">
<LinearLayout
app:layout_constraintHeight_percent="0.5">
</LinearLayout>
</LinearLayout>
精彩评论