Android Percentage Layout Height

后端 未结 5 919
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 03:49

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 o

相关标签:
5条回答
  • 2020-12-08 04:22

    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.

    0 讨论(0)
  • 2020-12-08 04:27

    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>
    
    0 讨论(0)
  • 2020-12-08 04:31

    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>
    
    0 讨论(0)
  • 2020-12-08 04:36

    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.

    0 讨论(0)
  • 2020-12-08 04:44

    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.

    0 讨论(0)
提交回复
热议问题