Android Layout: width half of parent

前端 未结 5 463
野的像风
野的像风 2021-02-02 06:36

I have a very simple layout that I can\'t make it looks like I want. It\'s a LinearLayout with a button and a Switch. I want them to show one above the other, but I want their w

相关标签:
5条回答
  • 2021-02-02 06:56
    Button button = (Button) findViewById(R.id.share_button);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    int buttonWidth = width/2;
    

    set this buttonWidth to your button like button.setWidth(buttonWidth);

    0 讨论(0)
  • 2021-02-02 06:57

    Try this code

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="2" >
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
    
                    <Switch
                        android:id="@+id/remember_me_switch"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:hint="@string/remember" />
    
                   <View 
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
    
                    <Button
                        android:id="@+id/share_button"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:onClick="loginOnclick"
                        android:text="@string/login_button_text" />
    
                   <View 
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                 </LinearLayout>
            </LinearLayout>
    
    0 讨论(0)
  • 2021-02-02 07:00

    One possible way is to have a master horizontal LinearLayout that splits the width to 2, and inside it have the vertical layout

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:orientation="vertical" >
    
                <Switch
                    android:id="@+id/remember_me_switch"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/remember" />
    
                <Button
                    android:id="@+id/share_button"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:onClick="loginOnclick"
                    android:text="@string/login_button_text" />
    
            </LinearLayout>
            <!-- Right side spacer -->
            <View
               android:layout_width="0dp"
               android:layout_height="1dp"
               android:layout_weight="1" />
    
        </LinearLayout>
    
    0 讨论(0)
  • 2021-02-02 07:00
    int params = linearLayout.getWidth()/2;
    
    ViewGroup.LayoutParams( params, ViewGroup.LayoutParams.WRAP_CONTENT));
    
    0 讨论(0)
  • 2021-02-02 07:03

    The trick is to use 0dp for the size you want manage via layout_weight! Try this

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2" >
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_weight="1"
                android:weightSum="2" >
    
                <Switch
                    android:id="@+id/remember_me_switch"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:hint="@string/remember" />
    
                <Button
                    android:id="@+id/share_button"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:onClick="loginOnclick"
                    android:text="@string/login_button_text" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_weight="1" >
            </LinearLayout>
    
        </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题