以编程方式设置新视图的边距

发布于 2024-09-16 23:21:48 字数 1619 浏览 5 评论 0原文

我有一个在 XML 布局中创建的 FrameView,在我的代码中,我尝试创建一系列新的 ImageView 并将它们添加为该 FrameView 的子级。 ImageView 很小,只有大约 15 像素正方形,我希望它们显示在 FrameView 周围的各个位置(我正在尝试实现看起来像雷达屏幕的东西)。我能够创建它们并添加它们,然后它们就会显示在屏幕上。然而,当我尝试设置它们的边距时,它似乎没有任何效果。无论我设置什么,所有 ImageView 都会显示在 FrameView 的左上角,而不是偏移适当的边距。下面列出了我的 XML 布局以及生成子视图的代码。我做错了什么吗?如何才能正确显示边距?或者有没有比使用边距更好的方法来做到这一点。

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/RadarBackground"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/radar_bg">
 <FrameLayout 
   android:id="@+id/RadarFrame" 
   android:layout_width="320dip" 
   android:layout_height="320dip" 
   android:layout_marginTop="25dip">
 </FrameLayout>
</LinearLayout>

Java:

for (int i = 0; i < getData().getTargetCount(); i ++) {
 int id = getData().getTargetId(i);
 Log.d(T.TAG_A, "Radar: plotting target " + id);
 TargetView tv = new TargetView(this, id, getData().getTargetName(id));
 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
 lp.setMargins(
   radarCenterX + (int) (getData().calcTargetRadarX(id, radarSize) / radarScale) - (targetSizeX / 2), 
   radarCenterY - (int) (getData().calcTargetRadarY(id, radarSize) / radarScale) - (targetSizeY / 2), 
   0, 0);
 tv.setImageResource(R.drawable.radar_civilian);
 tv.setOnClickListener(this);
 mTargets.add(tv);
 mFrame.addView(tv, lp);
}

I have a FrameView that's created in my XML layout, and in my code, I'm trying to create a series of new ImageViews and add them as children of this FrameView. The ImageViews are small, only about 15 pixels square, and I want them to show up in various positions around the FrameView (I'm trying to implement what looks like a radar screen). I'm able to create them and add them just fine, and they show up on the screen. However, when I try to set their margins, it doesn't seem to have any effect. No matter what I set, all the ImageViews show up in the top left corner of the FrameView, as opposed to offset by the appropriate margins. My XML layout is listed below, along with the code that generates the child views. Am I doing something wrong? How can I get the margins to show up properly? Or is there a better way to do this than by using margins.

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/RadarBackground"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/radar_bg">
 <FrameLayout 
   android:id="@+id/RadarFrame" 
   android:layout_width="320dip" 
   android:layout_height="320dip" 
   android:layout_marginTop="25dip">
 </FrameLayout>
</LinearLayout>

Java:

for (int i = 0; i < getData().getTargetCount(); i ++) {
 int id = getData().getTargetId(i);
 Log.d(T.TAG_A, "Radar: plotting target " + id);
 TargetView tv = new TargetView(this, id, getData().getTargetName(id));
 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
 lp.setMargins(
   radarCenterX + (int) (getData().calcTargetRadarX(id, radarSize) / radarScale) - (targetSizeX / 2), 
   radarCenterY - (int) (getData().calcTargetRadarY(id, radarSize) / radarScale) - (targetSizeY / 2), 
   0, 0);
 tv.setImageResource(R.drawable.radar_civilian);
 tv.setOnClickListener(this);
 mTargets.add(tv);
 mFrame.addView(tv, lp);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

起风了 2024-09-23 23:21:48

如果不指定视图重力,FrameLayout 不会考虑边距参数。尝试为所有 ImageView 指定重力。

哈!

FrameLayout does not consider the margin parameters if you don't specify the view Gravity. Try specifying the gravity for all the ImageViews.

HTH !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文