【发布时间】:2015-08-22 22:01:34
【问题描述】:
LinearLayout usersPlaceholder = Main.lay_found_users; //Found Users Placeholder
//RelativeLayout Params
RelativeLayout userlay = new RelativeLayout(mCtx); //Create new Element
userlay.setBackgroundResource(R.drawable.btn_useritem); //Background
userlay.setGravity(RelativeLayout.CENTER_HORIZONTAL); //Gravity
userlay.setClickable(true); //Clickable
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, dp(80));
relativeParams.setMargins(dp(80), dp(80), dp(80), dp(80));
userlay.setLayoutParams(relativeParams);
userlay.requestLayout();
usersPlaceholder.addView(userlay);
边距未设置。作为子元素的RelativeLayout 正在扩展直到匹配作为LinearLayout 的父usersPlaceholder。我也尝试为父元素设置填充,但同样的问题......
此方法转换度量
public int dp(int dps){
final float scale = mCtx.getResources().getDisplayMetrics().density;
return (int) (dps * scale + 0.5f);
}
主要活动:
public class Main extends Activity{
Context ctx;
public static LinearLayout lay_found_users;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
lay_found_users = (LinearLayout) findViewById(R.id.lay_found_users);
}
}
建议?谢谢。
【问题讨论】:
-
不要调用 requestLayout() 系统会为你调用
-
好的,谢谢,我打电话给它是因为我读到它可能是一个解决方案,但是无论调用还是不调用都不能使边距起作用:/
-
分享更多代码。这段代码非常简单,我不在这里。 Main.lay_found_useres。分享这个观点
-
Main 是主要的
Activity,lay_found_users 只是父LinearLayout声明为静态的。分享更多代码。
标签: android android-relativelayout padding margins