【问题标题】:Changing Buttons from Relative Layout to Linear Layout weird side-effects将按钮从相对布局更改为线性布局奇怪的副作用
【发布时间】:2012-08-06 16:21:44
【问题描述】:

今天发生在我身上最奇怪的事情,导致了数小时的沮丧和愤怒管理问题。

我有一个可以运行的 Android 幻灯片应用程序。但是,我对按钮(在相对布局中)不满意,并将它们移动到线性布局。但是,在运行我的应用程序时,按钮的行为被交换了(即按钮 1 完成了按钮 2 的工作,反之亦然)。我可以通过更改其中一个按钮的 id 来解决这个问题,而不是全部(这不起作用)。与此相关的是,当我切换方向时,我的应用程序开始崩溃。当我有相对布局时,它以前工作过。 (我正在使用 Handler 来更改幻灯片的 UI)

我无法解决这个问题,所以我恢复到我的相对布局,但我想知道这首先是怎么发生的。

有效的布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:contentDescription="@string/imageView"
        android:scaleType="centerInside"
        android:src="@drawable/image1" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />



        <ImageButton
            android:id="@+id/startStop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:contentDescription="@string/startStop"
            android:src="@drawable/ic_menu_slideshow" />

        <ImageButton
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="false"
            android:layout_toRightOf="@+id/startStop"
            android:contentDescription="@string/next"
            android:src="@drawable/ic_media_next" />

        <ImageButton
            android:id="@+id/previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="false"
            android:layout_toLeftOf="@+id/startStop"
            android:contentDescription="@string/previous"
            android:src="@drawable/ic_media_previous" />

</RelativeLayout>

导致问题的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >



<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:contentDescription="@string/imageView"
    android:scaleType="centerInside"
    android:src="@drawable/image1" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="#30eee9e9" >

            <ImageButton
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/previous"
        android:src="@drawable/ic_media_previous" />

    <ImageButton
        android:id="@+id/bla"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/startStop"
        android:src="@drawable/ic_menu_slideshow" />

    <ImageButton
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/next"
        android:src="@drawable/ic_media_next" />

【问题讨论】:

  • 你能在你处理按钮点击的地方发布你的布局xml和代码吗?
  • 你一定使用过相对布局的 LeftOff 或 RightOff 功能(作为 PARENT),是这样吗?
  • 我猜您的应用程序在方向更改后由于 ClassCast 或 NullPointer 异常而崩溃。你有不同的纵向和横向布局吗?如果是这样,您需要更新两个布局。但如果没有相关的 codelogcat 错误,我们只能猜测。
  • 为了了解它崩溃的原因,您需要查看 logcat。转到 Windows--> Show View --> Logcat 并查找红色异常区域。如果您单击带有您的包名称的行,它将显示错误提示。也可能想发布错误日志。
  • 顺便说一句,上面的评论有 +4 的原因是因为包括我自己在内的 4 位读者认为您应该发布更多信息。

标签: android xml multithreading layout


【解决方案1】:

我遇到了同样的问题,但是将相对布局作为父布局,将线性布局作为子布局,我采用了四个图像按钮

  • 播放
  • 得分
  • 帮助
  • 退出

并且按钮功能被转移到下一个,即当我按下 PLAY 时,按下 SCORE 时没有任何反应 点击 HELP 时游戏开始播放,显示得分等等。关闭大坝的东西试图看电影,但有一段时间我看它感到厌烦,我再次启动了 Eclipse,神奇的是它运行得如此之好。不知道,但正如 DeeV 所说,Rebuild & Clean 解决了我的问题

【讨论】:

  • 这并不是真正的答案,而是确认 Deev 的评论对您有所帮助。如果您发现 Deev 的评论有帮助,您会想要简单地投票!
猜你喜欢
  • 2017-10-01
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多