RelativeLayout weight

前端 未结 4 551
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 23:25

In a layout resource XML, I have 3 RelativeLayout(s) which are inside a main RelativeLayout. The view will be shown vertically. These 3 RelativeLayout() are set next to each

相关标签:
4条回答
  • 2020-12-03 00:00

    You can use Horizontally oriented LinearLayout Manager in the Recycler View, and place each RelativeLayout in each item, of its Adapter.

    The Link: How to build a Horizontal ListView with RecyclerView?

    If your RelativeLayouts are set to a fixed width and height, that is to the size of the Screen, that you can get from DisplayMatrics, that will be OK.

    The Link: Get Screen width and height

    If the contents of your RelativeLayouts are different, then you can use getItemViewType() method.

    Please see: How to create RecyclerView with multiple view type?

    Happy Coding :-)

    0 讨论(0)
  • 2020-12-03 00:01

    RelativeLayout does not pay attention to android:layout_weight. (That's a property of LinearLayout.LayoutParams, but not of RelativeLayout.LayoutParams.)

    You should be able to get the layout you want with a much simpler view hierarchy. It's not clear what you are trying to do, since the last two RelativeLayouts are empty. If you need a purely vertical organization, I'd suggest using LinearLayout instead of RelativeLayout.

    EDIT Based on your edit, it looks like you want a horizontal layout of three compound views, each one clickable. I think something like the following will work:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <!-- First column -->
        <LinearLayout
            android:id="@+id/firstColumn"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            >
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="..." />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="text 1"
                . . . />
        </LinearLayout>
    
        <!-- Second column -->
        <LinearLayout . . . >
            . . .
        </LinearLayout>
    </LinearLayout>
    

    If the contents of the buttons aren't correct, you can replace the second-level LinearLayout views with RelativeLayout if that helps organize the layout better.

    0 讨论(0)
  • 2020-12-03 00:02

    RelativeLayouts do not support weight. You need to use a LinearLayout as a parent container if you want to use weights.

    0 讨论(0)
  • 2020-12-03 00:09

    Solution is very simple. I have been looking for weight distribution in relative layout.

    It's a small trick for all these kind situations.

    Use LinearLayout with android:orientation="horizontal"

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