2013-09-02 37 views
14

Cách đặt trọng lực trong lập trình trong relativelayout. Tôi có một layout XML với tên chat_viewer_message.xml như sau:Cách đặt trọng lực theo chương trình trong relativelayout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/background" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingRight="4dip" 
    android:paddingBottom="4dip" 
    android:gravity="left" 
    android:background="@drawable/chat_bg_in"> 

    <ImageView 
     android:id="@+id/avatar" 
     android:layout_width="32dip" 
     android:layout_height="32dip" 
     android:layout_marginLeft="4dip" 
     android:src="@drawable/avatar_1_1" 
     /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/avatar" 
     android:paddingLeft="4dip" 
     /> 

</RelativeLayout> 

Và mã xem trong chương trình như sau:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final int type = getItemViewType(position); 
     final View view; 
     if (convertView == null) { 
      final int resource = R.layout.chat_viewer_message; 
      view = activity.getLayoutInflater() 
        .inflate(resource, parent, false); 
      if (type == TYPE_MESSAGE) 
       ((TextView) view.findViewById(R.id.text)).setTextAppearance(
         activity, appearanceStyle); 
     } else 
      view = convertView; 



     final MessageItem messageItem = (MessageItem) getItem(position); 
     String name; 
     final String account = messageItem.getChat().getAccount(); 
     final String user = messageItem.getChat().getUser(); 
     final String resource = messageItem.getResource(); 
     final boolean incoming = messageItem.isIncoming(); 
     final String server_host = view.getResources().getString(
       R.string.server_host); 
     if (isMUC) { 
      name = resource; 
     } 


     if (incoming) { 
      view.setBackgroundResource(R.drawable.chat_bg_in); 
     } else { 
      // I WANT TO GRAVITY TO RIGHT. HOW TO CODE? THANKS 
      view.setBackgroundResource(R.drawable.chat_bg_out); 
     } 

     return view; 
} 

Xem trọng lực tại RelativeLayout với id = background, mặc định lực hấp dẫn là TRÁI, Vì vậy, tôi muốn nếu đến trọng lực giá trị tại relativelayout được RIGHT thay vì LEFT.

Cảm ơn.

Trả lời

19

Cast view để bố trí container, trong trường hợp này RelativeLayout, và sử dụng setGravity(int) vào nó:

if (incoming) { 
    view.setBackgroundResource(R.drawable.chat_bg_in); 
} else { 
    // I WANT TO GRAVITY TO RIGHT. HOW TO CODE? THANKS 
    view.setBackgroundResource(R.drawable.chat_bg_out); 

    ((RelativeLayout) view).setGravity(Gravity.RIGHT); 
} 

Một lời cảnh báo (từ tài liệu):

Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.

+0

Không lỗi: nhưng không có hiệu ứng. vẫn còn ở bên trái. Cảm ơn. –

0

Bạn có thể sử dụng:

RelativeLayout.LayoutParams paramas = new RelativeLayout.LayoutParams(); 
params.gravity = Gravity.RIGHT; 
view.setLayoutParams(params); 

Để biết thêm về thông số trọng lượng u có thể kiểm tra này: Gravity

+0

Lỗi: java.lang.ClassCastException: android.widget.AbsListView $ LayoutParams không thể truyền tới android.widget.RelativeLayout $ LayoutParams –

+0

bạn đang thổi phồng bố cục sai: final int resource = R.layout.chat_viewer_message; ID bố cục mục ListView của bạn là nền, đó là RelativeLayout phải không? – Jilberta

+0

Giải quyết, nền tôi di chuyển đến chế độ xem văn bản, id nào là văn bản –

9

Có hai cách chính để lập trình trọng lực trong một RelativeLayout. Bạn có thể đặt trọng lực cho tất cả các chế độ xem con (setGravity(int)) hoặc đặt trọng số riêng lẻ (params.addRule(int)). Lưu ý: Những phương pháp này không có sẵn cho LinearLayout s.

Để thiết lập mức độ nghiêm trọng cho tất cả trẻ em:

RelativeLayout relativeLayout = findViewById(R.id.myRelativeLaout); 
relativeLayout.setGravity(Gravity.CENTER); 

Để thiết lập mức độ nghiêm trọng cho một cái nhìn duy nhất trong một RelativeLayout:

MyView myView = findViewById(R.id.myView); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
myView.setLayoutParams(params); 

Nguồn:

5

Nó hoạt động hoàn hảo cho trường hợp của tôi:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 

    layoutParams.topMargin = 700;//in my case 
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);//in my case 
    varButton.setLayoutParams(layoutParams); 
+0

addRule là đúng cách, làm việc như một say mê, cảm ơn – Mercury

0

Xây dựng bố trí chat và thao tác nó lập trình & sDirectionHasmap chứa chỉ đạo của mỗi tin nhắn.

if (sDirectionHashMap.get(idsList.get(position)).equals("incoming")) { 
      holder.messageLayout.setGravity(Gravity.LEFT); 
     } else { 
      holder.messageLayout.setGravity(Gravity.RIGHT); 
     } 
Các vấn đề liên quan