setMargins方法不适用于自定义ViewGroup [英] setMargins method is not working on custom ViewGroup
问题描述
我正在一个项目中,我的工作是在主屏幕上创建一个滑动布局,当应用程序启动时它将部分出现在底部,我们可以滚动该布局.然后,我必须在其中添加子视图,即卡片.现在,我已经创建了一个自定义ViewGroup
,并将以编程方式添加它们.
I am working on a project where my portion of work is to create a sliding layout on main screen which will partially appear at bottom when the app will start and we can scroll that layout. And then I have to add child views inside of it which will be cards. Now for that I have created a custom ViewGroup
and I will add them programmatically.
我已经成功完成了大部分任务.
I have succeed in making most of the portion of the task.
我为滑动布局创建了自定义FrameLayout
.
I have created a custom FrameLayout
for sliding layout.
然后我用它并添加了XML.
Then I used it and added in 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"/>
现在,我创建了一个名为MyCardView
的Java类,该类扩展了ViewGroup
.
Now after that I have created a java class named MyCardView
which 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
中.
Then I am trying to add objects of MyCardView
into custom FrameLayout
of mine programmatically.
这是onCreate()
中执行任务的方法.将两个自定义ViewGroup
添加到一个LinearLayout
中.
Here is the method inside onCreate()
which does the task. Which is to add two custom ViewGroup
s into one 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的边距,但这似乎并不适用.请帮忙.
Now what happens is I am trying to set margin of ViewGroup but that doesn't seem to apply. Please Help.
推荐答案
我做的错误是,在我的代码中,我声明了ViewGroup.MarginLayoutParams layoutParams1
的实例,并且在linearLayout.addView(vg1,layoutParams1)
中传递了相同的实例,但是该方法addView()
接受View
作为第一个参数,并接受LayoutParams
的实例作为第二个参数.
The mistake I was doing is, in my code I have declared an instance of ViewGroup.MarginLayoutParams layoutParams1
and I am passing the same instance in linearLayout.addView(vg1,layoutParams1)
but the method addView()
accepts View
as first argument and an instance of LayoutParams
as second.
所以我认为这是问题所在(不完全确定).所以我将代码更改为
So I think that was the problem (not fully sure). So I changed my code to
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,200);
这篇关于setMargins方法不适用于自定义ViewGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!