Я работаю над проектом, где моя часть работы заключается в создании скользящего макета на главном экране, который частично появится внизу, когда приложение запустится, и мы можем прокрутить этот макет. И тогда я должен добавить в него детские представления, которые будут карточками. Теперь для этого я создал пользовательский ViewGroup
, и я добавлю их программно.setMargins метод не работает на пользовательской ViewGroup
Мне удалось выполнить большую часть задачи.
Я создал обычай FrameLayout
для раздвижной компоновки.
Затем я использовал его и добавил в XML.
<?xml version="1.0" encoding="utf-8"?>
<com.dhrumil.airhockey.OverflowView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/layer_list"
android:padding="5dp"
android:layout_height="match_parent"/>
Теперь после того, как я создал класс Java с именем MyCardView
который exends ViewGroup
.
public class MyCardView extends ViewGroup {
public MyCardView(Context context){
super(context);
init(context);
}
public MyCardView(Context context, AttributeSet attrs){
super(context,attrs);
init(context);
}
public MyCardView(Context context, AttributeSet attrs, int defStyleAttr){
super(context, attrs,defStyleAttr);
init(context);
}
public MyCardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
public void init(Context context){
}
@Override
protected void onLayout(boolean b, int l, int i1, int i2, int i3) {
// TODO Auto-generated method stub
final int count = getChildCount();
int curWidth, curHeight, curLeft, curTop, maxHeight;
//get the available size of child view
int childLeft = this.getPaddingLeft();
int childTop = this.getPaddingTop();
int childRight = this.getMeasuredWidth() - this.getPaddingRight();
int childBottom = this.getMeasuredHeight() - this.getPaddingBottom();
int childWidth = childRight - childLeft;
int childHeight = childBottom - childTop;
maxHeight = 0;
curLeft = childLeft;
curTop = childTop;
//walk through each child, and arrange it from left to right
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
//Get the maximum size of the child
child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST));
curWidth = child.getMeasuredWidth();
curHeight = child.getMeasuredHeight();
//wrap is reach to the end
if (curLeft + curWidth >= childRight) {
curLeft = childLeft;
curTop += maxHeight;
maxHeight = 0;
}
//do the layout
child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight);
//store the max height
if (maxHeight < curHeight)
maxHeight = curHeight;
curLeft += curWidth;
}
}
}
}
Тогда я пытаюсь добавить объекты MyCardView
в обычае FrameLayout
шахты программно.
Это метод внутри onCreate()
, который выполняет эту задачу. Который должен добавить два пользовательских ViewGroup
s в один LinearLayout
.
public void setupText(){
FrameLayout frame = (FrameLayout) findViewById(R.id.overflow_frame);
LinearLayout linearLayout = new LinearLayout(getBaseContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
ViewGroup vg = new MyCardView(getBaseContext());
ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,200);
vg.setBackgroundColor(Color.WHITE);
ViewCompat.setElevation(vg, 5);
TextView tvTemp = new TextView(getBaseContext());
tvTemp.setTextColor(Color.BLACK);
tvTemp.setText("Hello There");
vg.addView(tvTemp);
ViewGroup vg1 = new MyCardView(getBaseContext());
ViewGroup.MarginLayoutParams layoutParams1 = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,200);
layoutParams1.setMargins(0,10,0,0);
vg1.setBackgroundColor(Color.WHITE);
ViewCompat.setElevation(vg1, 5);
TextView tvTemp1 = new TextView(getBaseContext());
tvTemp1.setTextColor(Color.BLACK);
tvTemp1.setText("Hello There 2");
vg1.addView(tvTemp1);
linearLayout.addView(vg,layoutParams);
linearLayout.addView(vg1,layoutParams1);
frame.addView(linearLayout);
}
Теперь, когда я пытаюсь установить маржу ViewGroup, но это не похоже на применение. Пожалуйста помоги.