android下面的getId无法以编程方式工作

时间:2014-11-11 13:57:46

标签: android android-activity android-relativelayout

我有一些xml布局文件。这是我的布局代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RelativeLayout
    android:id="@+id/rot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#ff0000" >

    <ImageView
        android:id="@+id/btn_categorry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="12dp"
        android:background="@drawable/ic_launcher" />
</RelativeLayout>

我写了一些代码,可以通过编程方式添加布局。这是我的代码

staticlayout = new RelativeLayout(getApplicationContext());
            staticlayout.setBackgroundColor(Color.parseColor("#000000"));

            ImageView add_btn = new ImageView(getApplicationContext());
            add_btn.setBackgroundResource(R.drawable.ic_launcher);

            RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
            //
            //
            add_btn.setLayoutParams(parms2);
            add_btn.setOnClickListener(new listener());

            staticlayout.addView(add_btn);

            parms.addRule(RelativeLayout.BELOW, R.id.rot);
            staticlayout.setLayoutParams(parms);
            mainlayout.addView(static layout);

它工作正常。现在我想添加另一个布局,女巫将在布局下面,我以编程方式添加。 这是我的完整代码

public class MainActivity extends Activity {
RelativeLayout myImage, mainlayout;
private ImageView img;
RelativeLayout staticlayout;
RelativeLayout.LayoutParams parms;

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

    myImage = (RelativeLayout) findViewById(R.id.rot);
    mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);

    img = (ImageView) findViewById(R.id.btn_categorry);

    parms = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);


    img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            staticlayout = new RelativeLayout(getApplicationContext());
            staticlayout.setBackgroundColor(Color.parseColor("#000000"));

            ImageView add_btn = new ImageView(getApplicationContext());
            add_btn.setBackgroundResource(R.drawable.ic_launcher);

            RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
            //
            //
            add_btn.setLayoutParams(parms2);
            add_btn.setOnClickListener(new listener());

            staticlayout.addView(add_btn);

            parms.addRule(RelativeLayout.BELOW, R.id.rot);
            staticlayout.setLayoutParams(parms);
            mainlayout.addView(staticlayout);
            // parms.addRule(RelativeLayout.BELOW, R.id.rot);

        }
    });

}

class listener implements OnClickListener {

    @Override
    public void onClick(View v) {
        RelativeLayout.LayoutParams costomparam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout relativelayout = new RelativeLayout(getApplicationContext());
        relativelayout.setBackgroundColor(Color.parseColor("#AB3232"));

        ImageView add_btn = new ImageView(getApplicationContext());
        add_btn.setBackgroundResource(R.drawable.ic_launcher);

        RelativeLayout.LayoutParams imagelayoutparam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        imagelayoutparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        imagelayoutparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
        //
        //
        add_btn.setLayoutParams(imagelayoutparam);
        relativelayout.addView(add_btn);

        costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());
        relativelayout.setLayoutParams(costomparam);
        mainlayout.addView(relativelayout);

    }
}

}

目前此部分无效

costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());
我在做错了什么?如果有人知道解决方案,请帮助我 感谢

3 个答案:

答案 0 :(得分:1)

在你做之前:

staticlayout.addView(add_btn);

请尝试添加类似

的内容

staticlayout.setId(999);

答案 1 :(得分:0)

我认为您在致电setId之前忘记致电staticlayoutstaticlayout.getId()

在values文件夹中创建名为ids.xml的资源。 你的看起来应该与此相似:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="your_static_layout_id" type="id" />
</resources>

答案 2 :(得分:0)

我不确定,但你在哪里添加了staticLayout?确保在调用staticlayout.getId()之前添加了staticlayout,并且还为staticLayout设置了一个id(在代码片段中缺少),否则您所需的规则应该无法正常工作