以编程方式将对象与xml中创建的对象对齐

时间:2014-03-15 11:21:25

标签: android layout

XML:

 <RelativeLayout
        android:id="@+id/rlMainDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:background="#f1f1f1"
        android:padding="5dp" >


     <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLength="32"
        android:hint="Type..." />

     <LinearLayout
        android:id="@+id/llSeekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_below="@+id/et_name"
        android:background="#f1f1f1"
        android:padding="5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_valuetxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:textColor="#000000"
            android:text="Points:" />

        <SeekBar
            android:id="@+id/seekbar"
            android:layout_width="210dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:max="100"
            android:progress="10" />

        <TextView
            android:id="@+id/tv_seekbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:textSize="16dp"
            android:textColor="#000000"
            android:text="10" />

      </LinearLayout>

    <EditText
        android:id="@+id/et_notes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/llSeekBar"
        android:maxLength="150"
        android:hint="Add details" />

</RelativeLayout>

代码:

 RelativeLayout relativeLayout = new RelativeLayout(this);
         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
         relativeLayout.setPadding(5, 5, 5, 5);
         relativeLayout.setBackgroundResource(R.drawable.drw_newbucket_item);
         lp.addRule(RelativeLayout.BELOW, R.id.et_notes);

         ImageView iv=new ImageView(this);
         iv.setId(1);
         RelativeLayout.LayoutParams lpiv = new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
         iv.setBackgroundResource(R.drawable.ic_action_content_picture);
         lpiv.addRule(RelativeLayout.ALIGN_PARENT_LEFT);


         TextView tv=new TextView(this);
         RelativeLayout.LayoutParams lptv = new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
         lptv.addRule(RelativeLayout.RIGHT_OF, iv.getId());
         lptv.addRule(RelativeLayout.CENTER_VERTICAL, relativeLayout.getId());
         tv.setLayoutParams(lptv);
         tv.setTextSize(16);
         tv.setTextColor(Color.parseColor("#000000"));
         tv.setText("blabla");


         relativeLayout.addView(iv);
         relativeLayout.addView(tv);
         addContentView(relativeLayout, lp);

我想将此布局放在et_notes EditText下。

lp.addRule(RelativeLayout.BELOW, R.id.et_notes);似乎不起作用。如果布局显示在屏幕顶部,则完美创建布局。

1 个答案:

答案 0 :(得分:0)

嗯,我无法做到,但我设法以另一种方式做我想做的事。我创建了我想用xml编程创建的布局然后我膨胀了这个布局。这种方式最简单,我不需要以编程方式创建布局。

llMain是根布局的ID。然后我膨胀我想放在根布局中的布局。

llMain = (LinearLayout) findViewById(R.id.llMain);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final LinearLayout proof = (LinearLayout) inflater.inflate(R.layout.proof_newitem, null);
final TextView tv_proof = (TextView) proof.findViewById(R.id.tv_newitem);
tv_proof.setText(filename);
ImageView iv_proof = (ImageView) proof.findViewById(R.id.iv_newitem);
llMain.addView(proof);