So for example I have these buttons which are as follows:
| [button1] [button2] |
| [button3] [button4] |
But when I add more buttons, they don't fit and look like:
| [button1] [button2] |
| [button3] [button4] [but|
As the result, it scrolls horizontally.
I have tried using LayoutParams
for the layout weight but instead I made some buttons stretched out and some buttons smaller, and it eliminates the gap between the buttons. I also have tried ButtonBarLayout
but it changed the orientation of the buttons which is completely different than what I wanted.
Here is my attempt which always weights the elements regardless if they fit or not
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, (int) Utils.dp(getResources(), 96));
params.weight = 1F;
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++) {
View view = this.getChildAt(i);
view.setLayoutParams(params);
}
}
And this is the XML (the button row that's too long to fit):
<com.tb24.fn.view.LockerRowLayout
android:id="@+id/locker_wrap_slots"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/locker_slot_wrap1"
layout="@layout/slot_view" />
<include
android:id="@+id/locker_slot_wrap2"
layout="@layout/slot_view" />
<include
android:id="@+id/locker_slot_wrap3"
layout="@layout/slot_view" />
<include
android:id="@+id/locker_slot_wrap4"
layout="@layout/slot_view" />
<include
android:id="@+id/locker_slot_wrap5"
layout="@layout/slot_view" />
<include
android:id="@+id/locker_slot_wrap6"
layout="@layout/slot_view" />
<include
android:id="@+id/locker_slot_wrap7"
layout="@layout/slot_view" />
</com.tb24.fn.view.LockerRowLayout>
The LockerRowLayout
class extends LinearLayout
.
So in the example, I want to let the layout adjust the second row buttons, but let the first row be intact. I don't want to make it scroll and instead I want to make their widths smaller and truncate them dynamically, for example:
| [button1] [button2] |
| [but.1] [but.2] [but.3] |
您无需以编程方式设置动态宽度,而是可以在xml中一行内的所有按钮中使用android:layout_weight="1"
和android:layout_width="0dp"
来自动调整它们的大小以适合整个宽度(我的意思是没有溢出)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.