难以设置动态设置CustomView到RelativeLayout的中心
我有CustomView,它只是一些弧线形状。难以设置动态设置CustomView到RelativeLayout的中心
public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;
private Paint paint = new Paint();
public CustomDrawableView(Context context, int startA, int endA) {
super(context);
int x = 0;
int y = 0;
int width = 400;
int height = 400;
paint.setColor(Color.BLACK);
mDrawable = new ShapeDrawable(new ArcShape(startA, endA));
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
canvas.drawLine(0,0,400,0,paint);
canvas.drawLine(0, 0, 0, 400, paint);
canvas.drawLine(400,0, 400,400, paint);
}
}
然后我通过它的id从xml文件中调出我的线性布局,为它添加一个framelayout。之后,我添加CustomDrawableView并尝试使用重力居中。我经历了许多其他问题并尝试了这些解决方案,但它对我而言不起作用。如果值得注意的是,我注意到当我使用像我的布局中没有自定义视图的textview这样的常规视图时,它完美地居中。
public class MainActivity extends ActionBarActivity {
private RelativeLayout ll;
private CustomDrawableView testView;
private RelativeLayout.LayoutParams layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load the layout
LinearLayout linLayout = (LinearLayout)findViewById(R.id.ll);
linLayout.setOrientation(LinearLayout.VERTICAL);
/******** Experimental Code **********/
RelativeLayout rl = new RelativeLayout(this);
linLayout.addView(rl);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
CustomDrawableView arc = new CustomDrawableView(this,0,30);
CustomDrawableView arcTwo = new CustomDrawableView(this, 50, 30);
rl.setGravity(Gravity.CENTER_HORIZONTAL);
rl.addView(arc, lp);
rl.addView(arcTwo, lp);
rl.setBackgroundColor(Color.GRAY); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我使用框架布局,所以我可以躺在彼此的意见。
您是否尝试将rl
的layout_width和layout_height设置为match_parent?
试试这个代码
/******** Experimental Code **********/
RelativeLayout rl = new RelativeLayout(this);
LayoutParams parems = new LayoutParams(LayoutParams.MATH_PARENT,
LayoutParams.MATH_PARENT);
rl.setParameters(parems);
linLayout.addView(rl);
UPDATE:
请出示您的activity_main.xml中的代码,并注意您的API级别。如果是这样,我要运行你的代码。
否则,你是否尝试从onMeasure()设置布局大小?
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = 0;
switch (heightMode) {
case MeasureSpec.AT_MOST: // wrap_confent
heightSize = 400;
break;
case MeasureSpec.EXACTLY: // match_parent
heightSize = MeasureSpec.getSize(heightMeasureSpec);
break;
}
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = 0;
switch (widthMode) {
case MeasureSpec.AT_MOST: // wrap_confent
widthSize = 400;
break;
case MeasureSpec.EXACTLY: // match_parent
widthSize = MeasureSpec.getSize(widthMeasureSpec);
break;
}
setMeasuredDimension(widthSize, heightSize);
}
这里有几个问题。
首先,当您将它添加到名为“linLayout”的LinearLayout中时,您需要为您的RelativeLayout设置名为“rl”的布局参数。用下面的代码执行此操作:
RelativeLayout rl = new RelativeLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
linLayout.addView(rl, params);
你的另一个问题是,当你把你的customViews到RL最好是一个规则添加到布局PARAMS比使用重力。下面的代码演示了这一点:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
CustomDrawableView arc = new CustomDrawableView(this,0,30);
CustomDrawableView arcTwo = new CustomDrawableView(this, 50, 30);
rl.addView(arc, lp);
rl.addView(arcTwo, lp);
希望这有助于!
我也试过了,它仍然不起作用。我试图理解为什么。如果我使用自定义视图,是否应该重写? – user3288005 2014-09-03 08:38:34
我试过了,但它仍然不起作用。 – user3288005 2014-09-03 08:28:31