【问题标题】:GUI issue, touching "button2" executes "button1" action on some devicesGUI 问题,触摸“button2”在某些设备上执行“button1”操作
【发布时间】:2011-12-05 08:41:43
【问题描述】:

我的应用程序中有以下布局,最低 API 级别为 4,目标 API 级别为 8。事实是,在某些设备(例如 Xperia X10 mini)中,当我触摸 @+actBula/btnZoomOut 时,采取的操作是从其侧面的按钮@+actBula/btnZoomIn。而在 Xperia X8 和其他三星手机中,它运行良好。

Xperia X10 mini 的规格为 240 x 320 像素,2.55 英寸(约 157 ppi 像素密度)。 Xperia X8 为 320 x 480 像素,3.0 英寸(约 192 ppi 像素密度)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/linearLayout1" android:scrollbarAlwaysDrawVerticalTrack="true" android:layout_width="fill_parent">
  <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="fill_parent">
    <LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="left">
        <Button android:id="@+actBula/btnBack" android:layout_height="wrap_content" android:layout_width="wrap_content" android:paddingLeft="10dp" android:paddingTop="10dp" android:layout_marginLeft="10dp" android:background="@drawable/setabola48" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout5" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="right" android:layout_gravity="right" android:orientation="horizontal" android:layout_marginLeft="50dp">
        <Button android:id="@+actBula/btnZoomIn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/zoomp48" android:layout_marginTop="5dp"></Button>
        <Button android:id="@+actBula/btnZoomOut" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dp" android:background="@drawable/zoomm48" android:layout_marginTop="5dp"></Button>          
    </LinearLayout>
  </LinearLayout>
<ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="fill_parent" android:orientation="vertical">
        <TextView android:layout_width="wrap_content" (...) ></TextView>
    </LinearLayout>
</ScrollView>

这些是 onClick 监听器:

    final TextView myTextView = (TextView) findViewById(R.actBula.txtBula);

    Button btnZoomIn = (Button) findViewById(R.actBula.btnZoomIn);

    btnZoomIn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          myTextView.setTextSize(myTextView.getTextSize()+1);
        }
    });

    Button btnZoomOut = (Button) findViewById(R.actBula.btnZoomOut);

    btnZoomOut.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          myTextView.setTextSize(myTextView.getTextSize()-1);
        }
    });

【问题讨论】:

  • 你能把你添加onClick监听器的代码贴出来吗?另外,您是否尝试过在按下按钮时记录实际的方法调用,以确保这不是一个简单的逻辑错误?
  • 我没有记录,但由于相同的 apk 和 xml 布局在不同设备上的工作方式不同,我猜测这是像素密度问题。

标签: android user-interface


【解决方案1】:

我刚刚解决了这个问题,把这行:myTextView.setTextSize(myTextView.getTextSize()-1); 改成myTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, myTextView.getTextSize()-1);

getTextSize() 返回确切的像素大小。如果我指定我的单位,我会得到想要的结果。 感谢在这里帮助我的人。

【讨论】:

    【解决方案2】:

    这听起来像按钮的 id 在 R.java 中混淆了。在将应用上传到 Xperia X10 mini 之前,您是否运行过Clean...

    由于 Android 1.6 不太可能,但请尝试将目标 API 级别临时更改为 4 以在 Xperia X10 mini 上运行应用程序,看看会发生什么。

    【讨论】:

    • 感谢您的回答。我在编译之前运行了 Project>Clean。事实上,该应用程序是直接从两部手机上的 Android Market 下载的,所以它是同一个 apk。我在我朋友的 X10 mini 上测试过,当我再次通过他时我会测试它。
    【解决方案3】:

    您也可以尝试将带有 id 的按钮声明到标准位置。我的意思是,将"@+actBula/btnZoomIn" 更改为"@+id/btnZoomIn" 等等。

    还有一点需要澄清的是按钮获得的实际 ID。将一些打印输出添加到您的代码中,然后查看按钮 id 的 LogCat 将打印:

    final TextView myTextView = (TextView) findViewById(R.actBula.txtBula);
    
    Button btnZoomIn = (Button) findViewById(R.actBula.btnZoomIn);
    
    //That's what I added:
    Log.i("", "btnZoomIn.getId()=" + btnZoomIn.getId() + ", R.actBula.btnZoomIn=" + R.actBula.btnZoomIn);
    
    btnZoomIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          myTextView.setTextSize(myTextView.getTextSize()+1);
        }
    });
    
    Button btnZoomOut = (Button) findViewById(R.actBula.btnZoomOut);
    
    //That's what I added:
    Log.i("", "btnZoomOut.getId()=" + btnZoomOut.getId() + ", R.actBula.btnZoomOut=" + R.actBula.btnZoomOut);
    
    btnZoomOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          myTextView.setTextSize(myTextView.getTextSize()-1);
        }
    });
    

    【讨论】:

    • 我按照您所说的进行了登录,并且按钮 ID 匹配。我为模拟器尝试了不同的屏幕尺寸,现在我对问题有了更清晰的了解。在这张图片中:imageshack.us/f/708/myscreenw.png 缩小按钮会缩小,无论点击缩小,它都会放大。这与布局有关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2021-03-14
    相关资源
    最近更新 更多