[英]How to adjust the LinearLayout elements when the layout children don't fit?
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. 我尝试使用LayoutParams
作为布局权重,但我将一些按钮拉长了,将一些按钮缩小了,这消除了按钮之间的间隙。 I also have tried ButtonBarLayout
but it changed the orientation of the buttons which is completely different than what I wanted. 我也尝试了ButtonBarLayout
但是它改变了按钮的方向,这与我想要的完全不同。
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): 这是XML(按钮行太长,无法容纳):
<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
. LockerRowLayout
类扩展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"
来自动调整它们的大小以适合整个宽度(我的意思是没有溢出)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.