2

私はRelativeLayoutを持っていて、いくつかのtextViewを挿入したいと思います。

コードは次のとおりです。

 <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 に答える