Android:以编程方式在RelativeLayout中添加textView

时间:2012-05-30 10:45:16

标签: android android-layout textview

我有一个RelativeLayout,我想插入一些textViews。

以下是代码:

 <RelativeLayout
    android:id="@+id/JournalSearchListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="30dp"
    android:layout_below="@+id/JournalsSearchTextView"
    android:orientation="vertical" 
    android:cacheColorHint="#00000000">

    <ProgressBar
        android:id="@+id/JournalSearchProgressBar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

我是如何以编程方式执行此操作的:

RelativeLayout journals = (RelativeLayout) findViewById(R.id.JournalSearchListView);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
for (int i=0;i< authorNames.size();i++) {
    TextView tv = new TextView(this);
    tv.setId(i); 
    tv.setText(authorNames.get(i));
    tv.setTextColor(Color.BLACK);
    Integer a = tv.hashCode();
    map.put(a,authorNames.get(i));
    params1.addRule(RelativeLayout.BELOW, tv.getId());
    journals.addView(tv, params1);
    tv.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             System.out.println("Clicked "+ map.get(v.hashCode()) );    
         }
    });   
}

但问题是每个textView与其他textView的位置相同。

4 个答案:

答案 0 :(得分:5)

params1.addRule(RelativeLayout.BELOW, tv.getId());

您将它设置为低于自身?
如果你想让它们低于另外,就这样做:

RelativeLayout journals = (RelativeLayout);
findViewById(R.id.JournalSearchListView);
LinearLayout lL = new LinearLayout(context);
lL.setOrientation(LinearLayout.VERTICAL);

for (int i=0;i< authorNames.size();i++) {
    TextView tv = new TextView(this);
    tv.setId(i); 
    tv.setText(authorNames.get(i));
    tv.setTextColor(Color.BLACK);
    Integer a = tv.hashCode();
    map.put(a,authorNames.get(i));

    if(i!=0){
        params1.addRule(RelativeLayout.BELOW, i-1);
    }

    tv.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            System.out.println("Clicked "+ map.get(v.hashCode()) ); 
        }
    });

    lL.addView(tv);   

}

journals.addview(lL);

答案 1 :(得分:1)

params1.addRule(RelativeLayout.BELOW, tv.getId());
journals.addView(tv, params1);

这基本上说“将textview置于其下方”。这当然不起作用,因此所有视图都将采用RelativeLayout中的默认位置。使用之前添加的TextView的id代替(如果插入第一个元素,则使用进度条的id)

答案 2 :(得分:1)

// Every UI object must have a layout parameter
        int widthContent = RelativeLayout.LayoutParams.WRAP_CONTENT;
        int heightContent = RelativeLayout.LayoutParams.WRAP_CONTENT;

        // Create relative layout
        RelativeLayout.LayoutParams rlParamsName = new RelativeLayout.LayoutParams(widthContent,heightContent);
        rlParamsName.setMargins(10, 0, 0, 0);   

        // Create a linear layout to add new object as vertical
        LinearLayout lL = new LinearLayout(context);
        lL.setOrientation(LinearLayout.VERTICAL);

        for (int i=0;i< 3;i++) {

            // Every time create new object of text view 
            TextView objDonateTextView = new TextView(this);
            objDonateTextView.setLayoutParams(rlParamsName);
            objDonateTextView.setText("Donate");
            objDonateTextView.setId(i);
            objDonateTextView.setTypeface(null, Typeface.BOLD);
            objDonateTextView.setTextColor(0xFF000000);

            lL.addView(objDonateTextView);

        }

        //Add button number two to the activity.
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.row_special);   
        rl.addView(lL);

// vKj

答案 3 :(得分:0)

可以使用相同的addRule函数

 RelativeLayout layout = (RelativeLayout)findViewById(R.id.slidingDrawerContent);
 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
 RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );


    for (int i=0; i<4; i++) {
         Button btn = new Button(this);
         btn.setId(i);
         btn.setText("some_text");

         lp.addRule(RelativeLayout.RIGHT_OF, <Id>);

         layout.addView(tv2, lp); 
    }