【问题标题】:Android: Add textViews in RelativeLayout ProgrammaticallyAndroid:以编程方式在RelativeLayout中添加textViews
【发布时间】:2012-05-30 10:45:16
【问题描述】:

我有一个 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 的位置都与其他的相同。

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    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);
    

    【讨论】:

    • 好吧,你为什么还要使用 relativelayout?如果您只想将项目放在彼此下方,请使用线性布局。我的名字是安德斯,不是安德烈亚斯 :-)
    • 哦,对不起,安德斯,我的错误。我想使用相对布局,因为我的布局中有一个progressBar,它位于布局的中间,这在线性布局中是不可能的,对吧?
    • 然后在相对布局中添加一个linearlayout :)
    • 很好的答案安德斯,我喜欢这样,这解决了我的问题
    【解决方案2】:
    params1.addRule(RelativeLayout.BELOW, tv.getId());
    journals.addView(tv, params1);
    

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

    【讨论】:

    • 谢谢 Alex,但我正在尝试 Andreas 的方法,但没有成功。
    【解决方案3】:
    // 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

    【讨论】:

      【解决方案4】:

      同样可以使用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); 
          }
      

      【讨论】:

      • 基本上取消注释行 lp.addRule(RelativeLayout.RIGHT_OF, );并提供 因为正如您在 RelativeLayout 中所知道的那样,他的视图相对于其他视图对齐,因此您需要提供其他视图 id....
      • 谢谢,但这仍然不能解决我的问题,我正在动态创建 textViews 并修改他们的名字......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多