如何为代码中设置的参数设置布局单位?
我正在编写一个 Android 应用程序。我的主项目中有一项活动继承自图书馆项目中的一项活动。我在基本活动中有一个自定义标题栏,其中有一个按钮,它使用以下样式:
<style name="TitleButton">
<item name="android:padding">6dp</item>
<item name="android:layout_width">48dip</item>
<item name="android:layout_height">48dip</item>
<item name="android:layout_gravity">right</item>
</style>
工作正常。在我的子活动中,我想添加一个按钮。我可以添加按钮,它点击并工作正常,但看起来不对。我按如下方式添加按钮:
ImageView imgAdd = new ImageView(this, null, R.style.TitleButton);
imgAdd.setImageResource(R.drawable.add);
imgAdd.setClickable(true);
imgAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addGroup();
}
});
FrameLayout tb = (FrameLayout) this.findViewById(R.id.Header);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
tb.findViewById(R.id.TitleClose).getLayoutParams()
);
lp.gravity = Gravity.RIGHT;
lp.width = 48;
lp.height = 48;
lp.setMargins(0, 9, 68, 0);
imgAdd.setLayoutParams(lp);
tb.addView(imgAdd, 1);
请注意,我通过代码添加的“添加”按钮也是如此大,并且偏移太多。我想这一定是由于 这个答案 的评论中指出的,设置宽度和高度所以设置它们以像素为单位,而 XML 布局将它们设置为倾角。所以,我的问题是,在代码中设置布局参数时,如何设置单位,以便我可以将新的添加按钮设置为以倾角而不是像素为单位进行测量,以便它能够正确显示?
I am writing an Android app. I have an activity in my main project which inherits from an activity in my library project. I have a custom titlebar in the base activity which has a button, which uses the following style:
<style name="TitleButton">
<item name="android:padding">6dp</item>
<item name="android:layout_width">48dip</item>
<item name="android:layout_height">48dip</item>
<item name="android:layout_gravity">right</item>
</style>
Works fine. In my child activity, I want to add a button. I can add the button, and it clicks and works fine, but it LOOKS wrong. I am adding the button as so:
ImageView imgAdd = new ImageView(this, null, R.style.TitleButton);
imgAdd.setImageResource(R.drawable.add);
imgAdd.setClickable(true);
imgAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addGroup();
}
});
FrameLayout tb = (FrameLayout) this.findViewById(R.id.Header);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
tb.findViewById(R.id.TitleClose).getLayoutParams()
);
lp.gravity = Gravity.RIGHT;
lp.width = 48;
lp.height = 48;
lp.setMargins(0, 9, 68, 0);
imgAdd.setLayoutParams(lp);
tb.addView(imgAdd, 1);
Notice how the Add button which I've added through code is too big, and offset too much. I'm figuring it must be due to what was pointed out in the comments on this answer, that setting the width and height so sets them in pixels, where as the XML layout sets them in dip. So, my question is, when setting layout params in code, how can you set the unit, so that I may set my new add button to be measured in dip instead of in pixels so that it will display right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 DisplayMetrics 获取当前密度并用它来设置宽度,相应的高度和边距(参考此处:px= dp * (dpi / 160))
You can use DisplayMetrics to get current density and use it to set width, height and margins accordingly (reference here: px = dp * (dpi / 160))