2

我有一个 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

4 回答 4

4

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);
于 2012-05-30T10:50:49.750 回答
1
params1.addRule(RelativeLayout.BELOW, tv.getId());
journals.addView(tv, params1);

这基本上是说“将文本视图放在自身下方”。这当然是行不通的,所以所有的视图都将在RelativeLayout 中采用默认位置。使用您之前添加的 TextView的 id(或者如果您插入第一个元素,则使用进度条的 id)

于 2012-05-30T10:49:01.963 回答
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

于 2013-12-03T12:09:52.863 回答
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); 
    }
于 2012-05-30T10:48:05.463 回答