2014-12-24 110 views
0

我正面臨爲相對佈局設置邊距的問題。我嘗試了很多方法,但沒有任何幫助。如何在android中以編程方式設置相對佈局的邊距?

- 嘗試#1

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
          LayoutParams.WRAP_CONTENT,  
          LayoutParams.WRAP_CONTENT); 
params.setMargins(0, 10, 0, 0); 
_rlMain.setLayoutParams(params); 

- 嘗試#2

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams(); 
params.setMargins(0, 50, 0, 0); 
_rlMain.setLayoutParams(params); 

- 嘗試#3

RelativeLayout.LayoutParams par=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
par.setMargins(0, 50, 0, 0); 
_rlMain.setLayoutParams(par); 

- 嘗試#4

FrameLayout.LayoutParams _rootLayoutParams = new FrameLayout.LayoutParams(_rlMain.getWidth(), _rlMain.getHeight()); 
_rootLayoutParams.setMargins(0, 10, 0, 0); 
_rlMain.setLayoutParams(_rootLayoutParams); 

這是我的XML部分:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeLayout 
     android:id="@+id/main_camera_act_rl" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

      </RelativeLayout> 

</RelativeLayout> 
+0

顯示您的xml./。 –

+0

@MurtazaHussain我編輯了我的問題並添加了XML部分。請看看。我需要爲此佈局設置保證金頂部android:id =「@ + id/main_camera_act_rl」 –

+2

您的寬度和高度設置爲「MatchParent 「這不會考慮保證金。嘗試「WrapContent」高度或寬度,然後嘗試您的「 - 試#2」 –

回答

3

您的widthheight設置爲"MatchParent",這不會考慮到餘量。嘗試"WrapContent"heightwidth然後努力"-Try#2"

2

試試這個,希望它會幫助你,它的工作對我來說

RelativeLayout relativeLayout = new RelativeLayout(getActivity()); 
     RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(200, 80); 
     relativeParams.setMargins(20, 20, 20, 20); 
     relativeLayout.setLayoutParams(relativeParams); 
     relativeLayout.setBackgroundColor(getResources().getColor(R.color.green_color)); 
+0

您正在創建新的相關佈局,而不是在現有佈局上設置邊距。 –

+0

@Hanish我沒有使用線性佈局。 –

1

試試這個:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams(); 
      params.setMargins(50,50,50,50); 
      _rlMain.setLayoutParams(params); 
+0

它是如何差異到 - 第2號? –

+0

您只將邊距設置爲頂部,但可能無效,請嘗試使用(左側,頂部,右側,底部)邊距。 –

+0

並不重要,如果他只將保證金設置爲一方? –

3

如果你正在使用的RelativeLayout內的LinearLayout ,您需要使用LinearLayout.LayoutParams而不是RelativeLayout.LayoutParams。

因此,在上述情況下更換你的下面的代碼...

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams(); 
      params.setMargins(0, 50, 0, 0); 
      _rlMain.setLayoutParams(params); 

與...

LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
     new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT)); 
relativeParams.setMargins(0, 50, 0, 0); 
relativeLayout.setLayoutParams(relativeParams); 
relativeLayout.requestLayout(); 
1

這是你的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:id="@+id/rl" 
     android:background="@android:color/black" 
     android:layout_height="match_parent" > 
</RelativeLayout> 

這是你的Java文件:

RelativeLayout rl, rlCustom; 

rl = (RelativeLayout) findViewById(R.id.rl); 
rlCustom = new RelativeLayout(MainActivity.this); 

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50); 
params.setMargins(50, 50, 50, 50); 
rlCustom.setLayoutParams(params); 
rlCustom.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); 
rl.addView(rlCustom); 

你走了!

1

android.view.ViewGroup.LayoutParams的LayoutParams = image.getLayoutParams();

WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE); 

    Display display = wm.getDefaultDisplay(); 
    DisplayMetrics displaymatrics = new DisplayMetrics(); 
    display.getMetrics(displaymatrics); 

    int displayWidth=0; 
    try{ 
     Point size = new Point(); 
     display.getSize(size); 
     displayWidth=size.x; 
     //displayWidth = displaymatrics.widthPixels; 
    }catch(Exception e) 
    { 
     e.printStackTrace() 
    } 
    ((RelativeLayout.LayoutParams) layoutParams).setMargins(10, 10, 10, 10); 
    image.setLayoutParams(layoutParams); 
相關問題