2014-09-03 213 views
0

我有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); 
    } 
} 

我使用框架佈局,所以我可以躺在彼此的意見。

回答

0

您是否嘗試將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); 
} 
+0

我試過了,但它仍然不起作用。 – user3288005 2014-09-03 08:28:31

0

這裏有幾個問題。

首先,當您將它添加到名爲「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); 

希望這有助於!

+0

我也試過了,它仍然不起作用。我試圖理解爲什麼。如果我使用自定義視圖,是否應該重寫? – user3288005 2014-09-03 08:38:34