为什么我的编辑文本没有受到重力...?
我正在以编程方式添加一个编辑文本,因为我正在设置重力但它不反映。
代码:
EditText bcc = new EditText(getApplicationContext());
LayoutParams para = new LayoutParams(LayoutParams.FILL_PARENT, 45);
//bcc.setBackgroundColor(Color.parseColor("#00000000"));
bcc.setTextColor(Color.parseColor("#000000"));
bcc.setSingleLine(true);
para.setMargins(0, 0, 0, 5); // left, top, right, bottom.
bcc.setTextSize(15);
bcc.setGravity(Gravity.BOTTOM);
bcc.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
bcc.setId(100);
bcc.setLayoutParams(para);
I am adding one edit text pro-grammatically, in that i am setting the gravity but its not reflecting.
code:
EditText bcc = new EditText(getApplicationContext());
LayoutParams para = new LayoutParams(LayoutParams.FILL_PARENT, 45);
//bcc.setBackgroundColor(Color.parseColor("#00000000"));
bcc.setTextColor(Color.parseColor("#000000"));
bcc.setSingleLine(true);
para.setMargins(0, 0, 0, 5); // left, top, right, bottom.
bcc.setTextSize(15);
bcc.setGravity(Gravity.BOTTOM);
bcc.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
bcc.setId(100);
bcc.setLayoutParams(para);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此重力
bcc.setGravity(Gravity.BOTTOM);
仅标记文本在EditText
内的放置方式。如果 EditText 的父级是
RelativeLayout
,您可以在RelativeLayout.LayoutParams
内提供规则。This gravity
bcc.setGravity(Gravity.BOTTOM);
marks only how text should lay insideEditText
.If parent of EditText is
RelativeLayout
you can provide rules insideRelativeLayout.LayoutParams
.设置视图父级的重力。如果视图父级是布局那么代码将如下所示
set gravity of parent of the view. If view parent is layout then the code will be like the following
最终您会将此 EditText
bcc
添加到视图组吗?根据父 ViewGroup 的类型,您需要执行以下操作:LinearLayout:
通过 XML:
您必须设置 android:layout_gravity="center_vertical"。
通过代码:
对于不同的父布局类型,代码会有所不同。
Eventually you would be adding this EditText
bcc
to a view group? Depending on what type of ViewGroup the parent is, you would need to do the following:LinearLayout:
via XML:
You have to set android:layout_gravity="center_vertical".
via code:
The code will be different for different parent layout types.
以编程方式创建 EditText 时,必须首先设置 setKeyListener(TextKeyListener.getInstance());
否则你的视图将始终与 Gravity.TOP 对齐。
我不知道真正的原因,但在向 EditText 指定任何其他参数之前,必须设置 setKeyListener(TextKeyListener.getInstance());
更正:仅当您通过扩展 EditText 并在 XML 中定义小部件来创建自定义小部件时,它才有效。仅在Android 5.0.1上测试
When you creating EditText programmatically, you must at first set setKeyListener(TextKeyListener.getInstance());
Otherwise your view will always be aligned with Gravity.TOP.
I don't know real reason, but before you specifiy any other parameter to EditText, you must set setKeyListener(TextKeyListener.getInstance());
Correction : It only work if you create you custom widget by extending EditText and defining your widget in XML. Only tested on Android 5.0.1