【问题标题】:Layout API-dependent?布局 API 依赖?
【发布时间】:2012-03-17 19:40:56
【问题描述】:

我正在尝试执行的想法是打印吉他指板,并在其顶部放置一些方块来指向一些音符。嗯,一张图值一千字,所以this is it运行在Android 2.3.3

“你为什么需要我们的帮助,所以?!”因为我尝试在 Android 4.0.3 中运行它,结果是this other one。我意识到(只留下一个正方形)正方形试图尽可能大,所以我为每个点添加了 dot[counter].setScaleType(ScaleType.CENTER) 并得到了这个(http://i.imgur. com/4yOJD.png 抱歉,我不能发布另一个超链接),而 2.3.3 与第一次捕获相同。这使我无法将 setPadding() 普遍应用于每个点,因为它在两个版本中是不同的。不得不说它是完全一样的代码,只是在不同的 AVD 中运行(我也尝试过物理设备,结果相同)。

我也尝试在代码和 xml 中更改布局的重力,但没有效果。

我正在复制我认为与情况相关的内容,但请随时提出其他要求。

这是我的简单布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/add"
    android:textAppearance="?android:attr/textAppearanceLarge" />


<FrameLayout
    android:id="@+id/layoutFrets"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" >

    <ImageView
        android:id="@+id/imageView1"
        android:scaleType="fitXY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fretboard" />

</FrameLayout>

这里是生成和打印点的活动代码:

创建 ImageViews:

for (int i = 0; i<notesN; i++)
        dot[i] = new ImageView(this);

为了打印它们,我必须这样做,因为我需要布局的大小来打印点确定

fl.post(new Runnable(){
        public void run() {
            int counter = 0;

            //Pointless constants go here to print the dots in their place              

            for (int strings = 0; strings < fretboard.NSTRINGS; strings++){
                for (int frets = 0; frets < fretboard.NFRETS; frets++){

                    if (fretboard.isOccupied(strings, frets)){
                        dot[counter].setScaleType(ScaleType.CENTER);
                        dot[counter].setImageResource(R.drawable.dot);  

                        //Quite a messy code... calculates the positions based
                        //on the constants above
                        dot[counter].setPadding(
                                (int) (-fl.getWidth() + 2 * fl.getWidth()
                                        * fretDistance[frets]),
                                (int) (fl.getHeight() * (stringDistance[strings])),
                                0, 0);

                        fl.addView(dot[counter]);
                        counter++;
                    }
                }
            }

        }

    });

这将解决我给你的最后一次捕获。

我也尝试在 post() 方法之外初始化 ImageViews,结果相同。

知道我做错了什么吗?非常感谢你:)

【问题讨论】:

    标签: android layout imageview gravity android-framelayout


    【解决方案1】:

    我醒来时想到了改变布局类型的想法……它奏效了。

    将 FrameLayout 更改为相对布局,一切都按预期工作。

    我会继续关注这个帖子,以防有人解释为什么会发生这种情况。

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 2013-02-10
      • 1970-01-01
      • 2016-09-24
      • 2021-09-05
      • 2020-07-05
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多