我为你创建这个布局。您只需要创建一个XML文件并将项目放在此容器中。设置元素的重要性。如果没有足够的空间,重要性较低的项目将被删除。如果你需要补充或改变什么,请告诉我。在我的设备上它工作。
例子
<com.sup.dev.android.views.widgets.layouts.LayoutImportance
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/red_700"
android:text="text_1"
app:LayoutImportance_Layout_importance="5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue_700"
app:LayoutImportance_Layout_importance="1"
android:text="teeeeeeeeext_2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/green_700"
app:LayoutImportance_Layout_importance="2"
android:text="teeeeeeeeext_3_teeeeext"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/amber_700"
app:LayoutImportance_Layout_importance="3"
android:text="teeeeeeeeext_4_teeeeeeeeeeeeeeeeeeext"/>
</com.sup.dev.android.views.widgets.layouts.LayoutImportance>
layoutimportance.java
public class LayoutImportance extends LinearLayout {
private int lock;
public LayoutImportance(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (lock == 0) for (int i = 0; i < getChildCount(); i++) getChildAt(i).setVisibility(VISIBLE);
int w = MeasureSpec.getSize(widthMeasureSpec);
super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.UNSPECIFIED), heightMeasureSpec);
if (getMeasuredWidth() > w) {
ArrayList<View> children = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) if (getChildAt(i).getVisibility() == VISIBLE) children.add(getChildAt(i));
if(children.isEmpty())return;
Collections.sort(children, (o1, o2) -> ((LayoutParams) o1.getLayoutParams()).importance - ((LayoutParams) o2.getLayoutParams()).importance);
children.get(0).setVisibility(GONE);
lock++;
onMeasure(widthMeasureSpec, heightMeasureSpec);
lock--;
}
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(getContext(), null);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p.width, p.height);
}
public static class LayoutParams extends LinearLayout.LayoutParams {
public int importance;
public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LayoutImportance_Layout);
importance = a.getInt(R.styleable.LayoutImportance_Layout_LayoutImportance_Layout_importance, 0);
a.recycle();
}
public LayoutParams(int w, int h) {
super(w, h);
}
}
}
属性.xml
<declare-styleable name="LayoutImportance_Layout">
<attr name="LayoutImportance_Layout_importance" format="integer"/>
</declare-styleable>