相对布局父中心为真

时间:2014-01-08 08:20:00

标签: android android-layout

我现在正在实施拖拽和放大删除relative_layout。我有一个扩展Relativelayout的类。 当我将相对布局拖到另一个布局时,我想将相对布局自动放置在容器的中心(相对布局),编码如(android:layout_centerInParent =“true”from xml)。

这是我的类扩展相对布局:

    public class Test extends RelativeLayout {

    ImageView image2;
    public RL(Context context){

        super(context);

        TextView txtName= new TextView(context);
        TextView bac_Number= new TextView(context);
        ImageView image = new ImageView(context);
        ImageView image1 = new ImageView (context);
        ImageView image2 = new ImageView(context);


        txtName.setText("First");
       // bac.setId(1);

        txtName.setText("Aung Aung");
        image.setImageResource(R.drawable.fcb);
        image1.setImageResource(R.drawable.upload);
        image2.setImageResource(R.drawable.guest);
        bac_Number.setText("8");

        RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        addView(txtName, lpSecond);

        RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //lpFirst.addRule(RelativeLayout.RIGHT_OF, second.getId());
        addView(bac_Number, lpFirst);

        RelativeLayout.LayoutParams lpThird = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            addView(image, lpThird);
    }
}

这是我在课堂上面调用的活动

    public class MainActivity extends Activity {

    Button btn_Choose;
    int p;
    String[] series = {"Photo","Name","Back Number"};
    String[] series1 = {"myPhoto","GuestPhoto","Default"};
    GridLayout layoutmain;


    private static final String IMAGEVIEW_TAG = "The Android Logo";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        RL  r = new RL(this);

        RL  r1 = new RL(this);
        RL  r2 = new RL(this);
        RL  r3 = new RL(this);
        RL  r4 = new RL(this);
        setContentView(R.layout.activity_main);
        layoutmain = (GridLayout)findViewById(R.id.layoutmain);
        layoutmain.addView(r);
        r.setGravity(Gravity.CENTER);
        layoutmain.addView(r1);
        layoutmain.addView(r2);
        layoutmain.addView(r3);
        layoutmain.addView(r4);
        btn_Choose = (Button)findViewById(R.id.btnselect);

        r.setTag(IMAGEVIEW_TAG);
        r1.setTag(IMAGEVIEW_TAG);
        r2.setTag(IMAGEVIEW_TAG);
        r3.setTag(IMAGEVIEW_TAG);
        r4.setTag(IMAGEVIEW_TAG);

       // myImage.setOnLongClickListener(new MyClickListener());
        r.setOnLongClickListener(new MyClickListener());
        r1.setOnLongClickListener(new MyClickListener());
        r2.setOnLongClickListener(new MyClickListener());
        r3.setOnLongClickListener(new MyClickListener());
        r4.setOnLongClickListener(new MyClickListener());

        findViewById(R.id.toplinear).setOnDragListener(new MyDragListener());
        findViewById(R.id.bottomlinear).setOnDragListener(new MyDragListener());
        findViewById(R.id.bottomlinear1).setOnDragListener(new MyDragListener());
        findViewById(R.id.layoutlinear).setOnDragListener(new MyDragListener());
        findViewById(R.id.layoutmain).setOnDragListener(new MyDragListener());
    }

    private final class MyClickListener implements OnLongClickListener {

        @Override
        public boolean onLongClick(View view) {
        // TODO Auto-generated method stub

            ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());

            String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
            ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

            view.startDrag( data,
                            shadowBuilder,
                            view,
                            0
                          );
            return true;
        }
    }


    class MyDragListener implements OnDragListener {
        Drawable normalShape = getResources().getDrawable(R.drawable.normal_shape);
        Drawable targetShape = getResources().getDrawable(R.drawable.target_shape);

        @Override
        public boolean onDrag(View v, DragEvent event) {

            switch (event.getAction()) {

            case DragEvent.ACTION_DRAG_STARTED:
                    Toast.makeText(getApplicationContext(), "Start Drag ", Toast.LENGTH_LONG).show();
                break;

            case DragEvent.ACTION_DRAG_ENTERED:
                v.setBackground(targetShape);   //change the shape of the view
                break;


            case DragEvent.ACTION_DRAG_EXITED:
                v.setBackground(normalShape);   //change the shape of the view back to normal
                break;

            case DragEvent.ACTION_DROP:

                if(v.getClass().toString().equals("class android.widget.RelativeLayout")){
                    Log.i("class","Relative");

                      View view = (View) event.getLocalState();
                      ViewGroup viewgroup = (ViewGroup) view.getParent();
                      viewgroup.removeView(view);

                      RelativeLayout containView = (RelativeLayout) v;
                      containView.addView(view);
                      view.setVisibility(View.VISIBLE);
                  }
                else {

                      Log.i("CLAss", v.getClass()+"Cant drop");
                      View view = (View) event.getLocalState();
                      view.setVisibility(View.VISIBLE);
                      Context context = getApplicationContext();
                      Toast.makeText(context, "You can't drop the image here",
                                                 Toast.LENGTH_LONG).show();
                      break;
                   }
                  break;

            case DragEvent.ACTION_DRAG_ENDED:
                v.setBackground(normalShape);   //go back to normal shape

            default:
                break;
            }
            return true;
        }
    }

    public void goClick(View v){
        if (v.getId() == R.id.btnselect){



            RotationPickerDialog dp = new RotationPickerDialog(this, series, new RotationPickerListener() {

                @Override
                public void OnDoneButton(Dialog rotationdialog, String r, int position) {
                    // TODO Auto-generated method stub
                    rotationdialog.dismiss();
                    btn_Choose.setText(r);
                    p = position ;

                }
            });

            WindowManager.LayoutParams WMLP = dp.getWindow().getAttributes();

             WMLP.x = 100;
             WMLP.y = 250;

            dp.getWindow().setAttributes(WMLP);
            dp.show();
            Log.i("p", p+"");
            Toast.makeText(this, "Opponent Team Name", Toast.LENGTH_LONG).show();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以向RelativeLayout.LayoutParams添加规则:

RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)r.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT);

答案 1 :(得分:0)

试试这个......

Rl.setGravity(RelativeLayout.CENTER_IN_PARENT);