android:layout_height 50% of the screen size

前端 未结 10 866
无人共我
无人共我 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 16:47

    You should do something like that:

    <LinearLayout
        android:id="@+id/widget34"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_below="@+id/tv_scanning_for"
        android:layout_centerHorizontal="true">
    
        <ListView
            android:id="@+id/lv_events"
            android:textSize="18sp"         
            android:cacheColorHint="#00000000"
            android:layout_height="1"
            android:layout_width="fill_parent"
            android:layout_weight="0dp"
            android:layout_below="@+id/tv_scanning_for"
            android:layout_centerHorizontal="true"
            />
    
    </LinearLayout>
    

    Also use dp instead px or read about it here.

    0 讨论(0)
  • 2020-12-23 16:48

    it's so easy if you want divide your screen two part vertically ( top30% + bottom70%)

    <LinearLayout
                android:id="@+id/LinearLayoutTop"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="2">
    
         </LinearLayout>
         <LinearLayout
                android:id="@+id/LinearLayoutBottom"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1">
    
         </LinearLayout>
    
    0 讨论(0)
  • 2020-12-23 16:49

    Set its layout_height="0dp"*, add a blank View beneath it (or blank ImageView or just a FrameLayout) with a layout_height also equal to 0dp, and set both Views to have a layout_weight="1"

    This will stretch each View equally as it fills the screen. Since both have the same weight, each will take 50% of the screen.

    *See adamp's comment for why that works and other really helpful tidbits.

    0 讨论(0)
  • 2020-12-23 16:53

    You can use android:weightSum="2" on the parent layout combined with android:layout_height="1" on the child layout.

               <LinearLayout
                    android:layout_height="match_parent"
                    android:layout_width="wrap_content"
                    android:weightSum="2"
                    >
    
                    <ImageView
                        android:layout_height="1"
                        android:layout_width="wrap_content" />
    
                </LinearLayout>
    
    0 讨论(0)
  • 2020-12-23 16:54

    This is easy to do in xml. Set your top container to be a LinearLayout and set the orientation attribute as you wish. Then inside of that place two linearlayouts that both have "fill parent" on width and height. Finally, set the weigth attribute of those two linearlayouts to 1.

    0 讨论(0)
  • 2020-12-23 16:57

    This is my android:layout_height=50% activity:

    <?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" >
    
        <LinearLayout
            android:id="@+id/alipay_login"
            style="@style/loginType"
            android:background="#27b" >
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/taobao_login"
            style="@style/loginType"
            android:background="#ed6d00" >
        </LinearLayout>
    
    </LinearLayout>
    

    style:

    <style name="loginType">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">0.5</item>
        <item name="android:orientation">vertical</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题