android:layout_height 50% of the screen size

前端 未结 10 867
无人共我
无人共我 2020-12-23 16:29

I just implemented a ListView inside a LinearLayout, but I need to define the height of the LinearLayout (it has to be 50% of the screen height).



        
相关标签:
10条回答
  • 2020-12-23 17:03

    To achieve this feat, define a outer linear layout with a weightSum={amount of weight to distribute}.

    it 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.Another example would be set weightSum=2, and if the two children set layout_weight=1 then each would get 50% of the available space.

    WeightSum is dependent on the amount of children in the parent layout.

    0 讨论(0)
  • 2020-12-23 17:09

    To make sure the height of a view is 50% of the screen then we can create two sub LinearLayouts in a LinearLayout. Each of the child LinearLayout should have "android:layout_weight" of 0.5 to cover half the screen

    the parent LinearLAyout should have "android:orientation" set to vertical

    .

    .

    here is code for your reference.... this code contains two buttons of height half the screen

    <LinearLayout 
    android:orientation="vertical"
    android:layout_height="match_parent">
    
    <LinearLayout
    
        android:layout_weight="0.5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <Button
            android:padding="10dp"
            android:layout_weight="0.5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="button1"
            android:id="@+id/button1"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            />
    
        <Button
            android:padding="10dp"
            android:layout_weight="0.5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="button2"
            android:id="@+id/button2"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            />
    
        </LinearLayout>
    
    <LinearLayout
    
        android:layout_weight="0.5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
    
        </LinearLayout>
       </LinearLayout>
    
    0 讨论(0)
  • 2020-12-23 17:12

    best way is use

    layout_height="0dp" layout_weight="0.5"

    for example

    <WebView
        android:id="@+id/wvHelp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5" />
    
    <TextView
        android:id="@+id/txtTEMP"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:text="TextView" />
    

    WebView,TextView have 50% of the screen height

    0 讨论(0)
  • 2020-12-23 17:14

    This kind of worked for me. Though FAB doesn't float independently, but now it isn't getting pushed down.

    Observe the weights given inside the LinearLayout

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/andsanddkasd">
    
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/sharedResourcesRecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="4"
                    />
    
                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_gravity="bottom|right"
                    android:src="@android:drawable/ic_input_add"
                    android:layout_weight="1"/>
    
            </LinearLayout>
    

    Hope this helps :)

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