2014-03-19 60 views
3

我正在开发一个android应用程序。如何用RelativeLayout以编程方式设置textview的引力?

public class MyListAdapter extends BaseAdapter { 
    Context con; 
    private LayoutInflater layoutinf; 
    ArrayList<Boolean> itemChecked = new ArrayList<Boolean>(); 
    ArrayList<String> items_ = new ArrayList<String>(); 

    public MyListAdapter(ChatActivity context) { 
     con = context; 
    } 

    public int getCount() { 
     return item_id.size(); 
    } 

    public Object getItem(int position) { 
     return item_id.size(); 
    } 

    public long getItemId(int position) { 
     return item_id.get(position).hashCode(); 
    } 

    public View getView(final int position, View arg1, ViewGroup arg2) { 

     View v = arg1; 
     ViewHolder holder = null; 
     if (v == null) { 
      layoutinf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = layoutinf.inflate(R.layout.row_chat, null); 
      holder = new ViewHolder(); 

     // holder.tv_contact = (TextView) v.findViewById(R.id.phone_num); 
      holder.tv_sms_body = (TextView) v.findViewById(R.id.msg_body); 
      holder.tv_time = (TextView) v.findViewById(R.id.time); 

      v.setTag(holder); 
     } else { 
      holder = (ViewHolder) v.getTag(); 
     } 

     if(item_flag.get(position).equals("1")) 
     { 
      holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_green); 

     } 
        else if(item_flag.get(position).equals("0")) 
        { 
         holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_yellow); 

        } 


     //holder.tv_contact.setText("" + item_phone_num.get(position)); 
     holder.tv_sms_body.setText(item_msg_body.get(position)); 
     holder.tv_time.setText(item_time.get(position)); 

     return v; 
    } 
} 

布局文件如下::我通过虚报行如下移植ListView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 



<TextView 
    android:id="@+id/msg_body" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignRight="@id/phone_num" 
    android:layout_below="@id/phone_num" 
    android:text="SMS" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/time" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_below="@id/msg_body" 
    android:text="time" 
    android:textSize="12dp" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

如何设置TextView的权重编程在上面的代码中使用的RelativeLayout ???

+0

你想在TextView的文本有重力的权利,或TextViews的布局有布局重心吗? – NameSpace

+0

我希望TextView的布局具有重力权。 – user3381979

回答

4

您可以设置这样的..

RelativeLayout.LayoutParams params = new LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    textView.setLayoutParams(params); 
+0

@NameSpace你有没有看过xml文件,一个textview是右边,另一个是左边,那么它将如何刷新?? ..如果你想要的话,你可以使用layoutparams –

+0

对齐另一个视图,在这种情况下,查看xml,他们是在不同的行上,所以对齐父母权限会产生相同的效果。 – NameSpace

+0

让我们假设我没有任何处理TextView的ID为@ + id/time的XML对齐方式。我问了关于TextView msg_body的问题,它是在XML中左对齐的。我想使用我的代码在正确的位置,我该如何做到这一点? – user3381979

3

这里是设置TextView的重力的代码。

yourTextView.setGravity(Gravity.CENTER); 
+0

这导致Textview中的文本被居中对齐,而不是视图居中其中。 –

+0

但我想要完全对齐的TextView。 – user3381979

相关问题