2013-09-02 7 views
14

프로그래밍 방식으로 상대적인 크기로 중력을 설정하는 방법. 프로그래밍상대적으로 프로그래밍 방식으로 중력을 설정하는 방법

<?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> 

그리고 코드보기는 다음과 같습니다 :

@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; 
} 

는 ID = 배경, 기본과 RelativeLayout의에서 중력을 참조하십시오 나는 다음과 같이 이름 chat_viewer_message.xml와 XML 레이아웃을 가지고 중력은 LEFT입니다. 그래서 들어오고 싶다면 상대의 값 중력은 LEFT가 될 것입니다.

감사합니다. u는이 확인할 수 있습니다 중력 매개 변수에 대한 자세한 정보를 위해

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

을 :

답변

19

캐스트 view를, 그 위에 setGravity(int)를 사용

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

주의 사항 (문서의) :

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

오류가 아니지만 효과보기는 아닙니다. 여전히 왼쪽에. 감사. –

0

당신은 사용할 수 있습니다이 경우 RelativeLayout 컨테이너 레이아웃에 Gravity

+0

오류 : 자바.lang.ClassCastException : android.widget.AbsListView $ LayoutParams를 android.widget.RelativeLayout $ LayoutParams에 캐스팅 할 수 없습니다. –

+0

잘못된 레이아웃을 부풀게하고 있습니다. final int resource = R.layout.chat_viewer_message; 귀하의 ListView 항목 레이아웃 ID는 RelativeLayout 아닌 배경입니까? – Jilberta

+0

해결 됨, 배경은 텍스트 인 텍스트 뷰로 이동합니다. –

9

RelativeLayout에는 프로그래밍 방식으로 중력을 설정하는 두 가지 주요 방법이 있습니다. 모든 하위보기의 중력을 설정하거나 (setGravity(int)) 중력을 개별적으로 설정 (params.addRule(int)) 할 수 있습니다. 참고 : LinearLayout에는 이러한 방법을 사용할 수 없습니다.

모든 아이들을 위해 중력을 설정하려면

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

출처 :

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

RelativeLayout 내에서 단일 뷰의 중력을 설정하려면

5

그것은 내 경우를 위해 완벽하게 작동:

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은 올바른 방식으로 매력처럼 작동했습니다. 감사합니다. – Mercury

0

채팅 레이아웃을 구축하고 & sDirectionHasmap 각 메시지의 방향이 포함되어 프로그래밍 방식으로 조작.

if (sDirectionHashMap.get(idsList.get(position)).equals("incoming")) { 
      holder.messageLayout.setGravity(Gravity.LEFT); 
     } else { 
      holder.messageLayout.setGravity(Gravity.RIGHT); 
     } 
관련 문제