【问题标题】:android.widget.LinearLayout$LayoutParams class errorandroid.widget.LinearLayout$LayoutParams 类错误
【发布时间】:2012-01-19 16:00:14
【问题描述】:

我遇到了这个错误 android.widget.LinearLayout$LayoutParams。

使用此代码时。

public void modifyingTextViews(){//This sets up a margin for the textviews.
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        llp.setMargins(0, 30, 0, 30);
        textview1.setLayoutParams(llp);
        textview2.setLayoutParams(llp);
    }

它与我的触摸方法或我的加载方法冲突,后者由带有 2 个文本视图的视图翻转器和一个加载器组成。

据我从一个类似issue 的人那里了解到,这是因为我收到此错误可能是因为视图的排列。

现在我的问题是,如果我有不同的 xml,我如何才能知道哪个是我在视图翻转器上的主要视图。此外,如果能快速解决问题,我们也将不胜感激。

【问题讨论】:

  • 将来,最好显示您的堆栈跟踪和/或 logcat 输出,这样我们就可以确切地知道问题是什么......

标签: android android-layout android-view android-linearlayout


【解决方案1】:

如果不了解更多信息,我无法判断问题所在(您的 xml 代码将是一个开始)。既然你要求一个简单的修复......我只会在 XML 中定义所有内容。这样一来,一切都井井有条,您不必担心在运行时设置LayoutParams

<ViewFlipper android:id="@+id/flipper"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

    <!-- layout #1 -->
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <!-- layout #2 -->
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView android:id="@+id/textview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <!-- fill in the rest here -->

</ViewFlipper>

当然,这是一个假设的示例,您必须编写 XML 代码才能获得所需的布局,但希望这能让您很好地了解从哪里开始。完成所有设置后,只需在 onCreate() 方法中调用 setContentView() 即可初始化布局。

【讨论】:

  • 根据我的经验,用 XML 编写布局比在运行时动态创建它们更难出错 :)。很高兴它对你有用!
猜你喜欢
  • 2023-04-08
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多