Adjust width to half screen

前端 未结 5 1956
再見小時候
再見小時候 2021-01-05 05:27

I came across this link :

Assign width to half available screen width declaratively

How to do the same in relative layout? Here\'s the code: Trying linea

相关标签:
5条回答
  • 2021-01-05 06:06

    In addition to urSus's answer, it's better to use a Space class instead of View:

       <Space android:id="@+id/fakeView"
           android:layout_width="0dp"
           android:layout_height="0dp" 
           android:layout_centerInParent="true"/>
    
    0 讨论(0)
  • 2021-01-05 06:09

    This is a nice workaround if you precisely need half, when using RelativeLayout. What you do is, make a fake invisible view centered in parent, and have left button be alignParentLeft and alignRight to that fake view and the right button vice-versa

    <RelativeLayout 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content">
    
       <View android:id="@+id/fakeView"
           android:layout_width="0dp"
           android:layout_height="0dp" 
           android:layout_centerInParent="true"/>
       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignRight="@id/fakeView"
           android:layout_alignParentLeft="true"
           android:text="Left"/>
    
       <Button 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@id/fakeView"
          android:layout_alignParentRight="true"
          android:text="Right"/>
    
    </RelativeLayout>
    

    Its really clever.

    0 讨论(0)
  • 2021-01-05 06:11
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        plannerEditLayout = (RelativeLayout) findViewById(R.id.plannerLayoutEdit);
        plannerEditLayout.setGravity(Gravity.CENTER_VERTICAL);
        plannerEditLayout.setLayoutParams(new RelativeLayout.LayoutParams(
                (width / 2), height));
    

    I use the same code for LinearLayout. Havent tried it out myslef for RelativeLayout. Give it a shot. Comment if it doesn't work.

    0 讨论(0)
  • 2021-01-05 06:30

    Wrap the same thing with Relative layout which should work for you.

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="somebutton">
    
        <TextView android:layout_width="fill_parent"
                android:layout_height="Wrap_content"
                android:layout_weight="1">
    </LinearLayout>
    
    0 讨论(0)
  • 2021-01-05 06:33
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button android:layout_width="match_parent"
                 android:id="@+id/button"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="somebutton">
    
        <TextView android:layout_width="match_parent"
                 android:id="@+id/textview"
                android:layout_height="Wrap_content"
                android:layout_weight="1">
    </LinearLayout>
    

    In above example used LinearLayout, set widget layout_width to "match_parent" and provide weight(to specify area to be occupy).

    This solves your problem.

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