我面临着为相对布局设置保证金的问题。我尝试了很多方法,但没有什么可以帮助我。
- 尝试#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 :(得分:3)
如果您在LinearLayout中使用RelativeLayout,则需要使用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 :(得分:3)
您的width
和height
设置为"MatchParent"
,不考虑保证金。尝试"WrapContent"
至height
或width
然后尝试"-Try#2"
答案 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));
答案 3 :(得分:1)
试试这个:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams();
params.setMargins(50,50,50,50);
_rlMain.setLayoutParams(params);
答案 4 :(得分: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);
你去!!!!
答案 5 :(得分: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);