2016-06-10 7 views
2

프로그래밍 방식으로 relativeLayout에 여백을 추가하려고했습니다. 필자는 아래에 언급 된 코드를 writtern했다. 하지만 그것을 실행할 때 마진은 올바르게 작동합니다 (마진 오른쪽은 작동하지 않습니다). 이 같은Android : 왼쪽 및 오른쪽 여백이 프로그래밍 방식으로 함께 작동하지 않음

public static void setMargins (View v, int l, int t, int r, int b) { 
    if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { 
     ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams(); 
     p.setMargins(l, t, r, b); 
     v.requestLayout(); 
    } 
} 

설정 마진 :

setMargins(mainLayout,100,0,100,0); 

이 내 배치된다

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#ffffff" 
> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:id="@+id/contact_include_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <include layout="@layout/view_common_header_with_tittle" /> 
    </LinearLayout> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true"> 
    <RelativeLayout 
     android:id="@+id/mainLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     > 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginTop="@dimen/u_done_action_bottom_margin" 
      android:layout_marginRight="@dimen/u_common_margin_left" 
      android:layout_marginLeft="@dimen/u_common_margin_left" 
      android:orientation="vertical"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/u_widget_height" 
       android:id="@+id/showCountryDialog" 
       android:background="@drawable/edittext_input_background_not_focus" 
       android:layout_marginTop="@dimen/u_card_button_margin_botton"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Choose Country" 
        android:id="@+id/CountryDialogDefaultText" 
        android:textColor="#4d4d4d" 
        android:textSize="@dimen/u_common_text_size" 
        android:layout_centerVertical="true" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentStart="true" 
        /> 

       <ImageView 
        android:id="@+id/u_register_third_step_country_image" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@drawable/ic_menu_downarrow_grey" 
        android:layout_centerVertical="true" 
        android:layout_marginRight="@dimen/u_common_text_size" 
        android:visibility="gone" 
        android:layout_alignParentRight="true" 
        /> 
      </RelativeLayout> 


      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="@dimen/u_common_margin_left" 
       android:orientation="horizontal" 

       > 

       <TextView 
        android:id="@+id/u_call_center_t1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="#4d4d4d" 
        android:textSize="@dimen/u_common_text_size" 
        android:text="@string/u_call_center_phone" /> 

       <TextView 
        android:id="@+id/u_call_center_phone" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingLeft="10dp" 
        android:textColor="#4d4d4d" 
        android:textSize="@dimen/u_common_text_size" 
        /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       android:layout_marginTop="@dimen/u_spinner_margin_top" 
       > 

       <TextView 
        android:id="@+id/u_call_center_t2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="#4d4d4d" 
        android:textSize="@dimen/u_common_text_size" 
        android:text="@string/u_call_center_wh" /> 

       <TextView 
        android:id="@+id/u_call_center_wh" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingLeft="10dp" 
        android:textColor="#4d4d4d" 
        android:textSize="@dimen/u_common_text_size" /> 
      </LinearLayout> 




     </LinearLayout> 


    </RelativeLayout> 
    </ScrollView> 
</LinearLayout> 

가 어떻게이 문제를 해결할 수

나는 다음과 같은 코드를 사용하고? 모두 감사합니다

+0

'setMargins (mainLayout, 100,0,10) 당신의 코드에서 : 'yourlayout.setLayoutParams (params);' –

답변

0
당신은 밀도 픽셀 (DP) 픽셀 (PX)보다 더 가치 평가자이 기능을 사용하여 전달해야

:

public static int dpToPx(int dp) { 
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density); 
} 

과 같이 설정 마진 함수를 호출 :

setMargins(mainLayout,dpToPx(100,0,dpToPx(100),0); 

및 상대 레이아웃의 경우 프로그램 적으로 여백을 설정하려면 다음과 같이하십시오.

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
       params.setMargins(0, dpToPx(10), 0, 0); 
       v.setLayoutParams(params); 
       v.requestLayout(); 
+0

java.lang.ClassCastException : android.widget.FrameLayout $ LayoutParams는 android.widget.RelativeLayout에 캐스팅 될 수 없습니다. LayoutParams @ Kapil Rajput – Georgo

+0

@ Georgo 코드를 정리하고 다시 작성하십시오. –