[英]How to set Margin for Relative Layout programmatically in android?
I am facing an issue to set Margin for Relative Layout. 我在为相对布局设置边距时遇到问题。 I have tried so many ways, but nothing helps me. 我已经尝试了很多方法,但是没有任何帮助。
-Try#1 -尝试#1
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 10, 0, 0);
_rlMain.setLayoutParams(params);
-Try#2 -尝试#2
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams();
params.setMargins(0, 50, 0, 0);
_rlMain.setLayoutParams(params);
-Try#3 -尝试#3
RelativeLayout.LayoutParams par=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
par.setMargins(0, 50, 0, 0);
_rlMain.setLayoutParams(par);
-Try#4 -尝试#4
FrameLayout.LayoutParams _rootLayoutParams = new FrameLayout.LayoutParams(_rlMain.getWidth(), _rlMain.getHeight());
_rootLayoutParams.setMargins(0, 10, 0, 0);
_rlMain.setLayoutParams(_rootLayoutParams);
Here is my XML part: 这是我的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>
if you are using RelativeLayout inside LinearLayout, you need to use LinearLayout.LayoutParams instead of RelativeLayout.LayoutParams. 如果在LinearLayout中使用RelativeLayout,则需要使用LinearLayout.LayoutParams而不是RelativeLayout.LayoutParams。
So in above case replace your following code... 因此,在上述情况下,请替换以下代码...
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams();
params.setMargins(0, 50, 0, 0);
_rlMain.setLayoutParams(params);
with... 与...
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();
Your width
and height
is set to "MatchParent"
which will not take margin into considerations. 您的width
和height
设置为"MatchParent"
,这不会考虑边距。 Try "WrapContent"
to height
or width
then try your "-Try#2"
尝试将"WrapContent"
为height
或width
然后尝试使用"-Try#2"
Try this hope it will help you,its working for me 尝试这个希望对您有帮助,对您有所帮助
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));
try this: 尝试这个:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams();
params.setMargins(50,50,50,50);
_rlMain.setLayoutParams(params);
This is your XML : 这是您的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>
This is you Java file : 这是您的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);
There you go!!! 你去了!!!
android.view.ViewGroup.LayoutParams layoutParams = image.getLayoutParams(); 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.