RelativeLayout中的两个小部件不在正确的位置

时间:2014-03-20 11:00:51

标签: android relativelayout

我想在android中创建一个imageView和一个textView,只有当用户点击了另一个活动的按钮并且当用户点击另一个活动时出现另一个按钮时才显示来自同一活动的按钮。

我使用相对布局以编程方式实现它:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* Adding the icon and text */
        // Creating a new RelativeLayout
        RelativeLayout relativeLayout = new RelativeLayout(this);

        // Defining the RelativeLayout layout parameters.
        // In this case I want to fill its parent
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);


        // Creating the icon
        ImageView icon = new ImageView(this);
        icon.setImageResource(R.drawable.add_16);

        // Defining the layout parameters of the ImageView
        RelativeLayout.LayoutParams iconlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        iconlp.addRule(RelativeLayout.CENTER_IN_PARENT);
        icon.setClickable(true);

        // Setting the parameters on the ImageView
        icon.setLayoutParams(iconlp);

        // Adding the ImageView to the RelativeLayout as a child
        relativeLayout.addView(icon);


        // Creating the textView
        TextView add = new TextView(this);
        add.setText("Add");

        // Defining the layout parameters of the TextView
        RelativeLayout.LayoutParams addlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        addlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        addlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
         add.setClickable(true);

        // Setting the parameters on the TextView
        addCard.setLayoutParams(addlp);

        // Adding the TextView to the RelativeLayout as a child
        relativeLayout.addView(add);

问题是图标出现在屏幕中间,文字是"添加"在它的底部,虽然我想把它放在图标下方......我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

这样的事情:

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

    /* Adding the icon and text */
    // Creating a new RelativeLayout
    final RelativeLayout relativeLayout = new RelativeLayout(this);

    // Defining the RelativeLayout layout parameters.
    // In this case I want to fill its parent
    final RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    rlp.addRule(RelativeLayout.CENTER_IN_PARENT);

    final TextView view = new TextView(this);
    view.setText("Add");
    view.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.add_16, 0, 0);
    view.setGravity(Gravity.CENTER);

    relativeLayout.addView(view, rlp);
    setContentView(relativeLayout);
}