如何使用.AddRule (RelativeLayout.CENTER_VERTICAL);
以编程方式将密码移至中心?
LinearLayout PO = (LinearLayout)RV .findViewById(R.id.LL1);
EditText ET = new EditText(getActivity());
ET.setId(1);
ET.setHint("Your name");
ET.setInputType(0x00000061);
ET.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
ET.setPadding(40,20,40,20);
ET.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
//ET.addRule(RelativeLayout.CENTER_VERTICAL);
PO.addView(ET);
.addRule
点亮。
答案 0 :(得分:0)
您可以使用gravity属性,但如果要在视图中添加更多组件,这将无用。您也可以使用RelativeLayout而不是LinearLayout(您需要更改布局),这可以帮助您做到这一点。
答案 1 :(得分:0)
XML:
<LinearLayout
android:id="@+id/xxx"
android:gravity="center_vertical">
或代码:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
layout.setGravity(Gravity.CENTER_VERTICAL);
或
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
xyzView.setLayoutParams(params);
答案 2 :(得分:0)
在以编程方式执行此操作之前,您最好尝试在layout.xml中构建UI,然后以编程方式执行此操作。
layout.xml中应该有相对布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
...
您的edittext应位于相对布局内,以便将RelativeLayout.LayoutParams应用于它。你的代码看起来像这样:
RelativeLayout PO = (RelativeLayout)RV .findViewById(R.id.RL1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
ET.setLayoutParams(params);
您也可以在以下链接中找到最后答案: http://www.coderanch.com/t/505316/Android/Mobile/put-layout-middle-screen