我正面臨爲相對佈局設置邊距的問題。我嘗試了很多方法,但沒有任何幫助。如何在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>
顯示您的xml./。 –
@MurtazaHussain我編輯了我的問題並添加了XML部分。請看看。我需要爲此佈局設置保證金頂部android:id =「@ + id/main_camera_act_rl」 –
您的寬度和高度設置爲「MatchParent 「這不會考慮保證金。嘗試「WrapContent」高度或寬度,然後嘗試您的「 - 試#2」 –