【问题标题】:Checkbox style in listview is incorrect列表视图中的复选框样式不正确
【发布时间】:2023-03-13 17:15:01
【问题描述】:

我的复选框的样式出现在列表视图中。我的应用程序使用的主题是 Holo,但复选框以旧样式显示。出现在别处的复选框看起来不错。我没有做任何花哨的事情,比如创造自己的风格。截图:

复选框应该看起来像右图中的那些。这是在 v4.0.3 上。这些复选框看起来应该在我尝试使用 v4.4.2 的另一台设备上。

列表视图行的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <CheckBox android:id="@+id/ListRow_Task_TaskComplete" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="6sp"
        android:focusable="false" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingTop="7dip"
        android:paddingBottom="7dip"
        android:layout_toRightOf="@+id/ListRow_Task_TaskComplete"
        android:layout_centerVertical="true" >

        <TextView android:id="@+id/ListRow_Task_Name" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textSize="16sp" />

        // next two textviews are programmatically hidden right now
        <TextView android:id="@+id/ListRow_Task_Deadline" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="12sp" />

        <TextView android:id="@+id/ListRow_Task_Tags" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/icon_tag"
            android:drawablePadding="4dip"
            android:textSize="12sp" />  

    </LinearLayout>

</RelativeLayout>

如何使左侧的复选框看起来像它们应该的样子?

【问题讨论】:

  • 你找出主要原因了吗?!我有类似的问题,我只是想知道哪款手机让你很有趣.. 三星 Galaxy S4 就是..
  • 我不完全记得原因,或者我什至想不通。我刚刚使用了 Holo 的复选框艺术的副本。对不起...
  • 我使用的手机看起来不正确的是 Galaxy S2,而正常工作的是 Nexus 7

标签: android android-listview android-styles android-checkbox


【解决方案1】:

从所有 dpi 和分辨率的 android drawable 文件夹中复制 Holo 复选框图像。然后将 android:button 属性设置为新的选择器 xml。这样您就可以完全控制它在目标设备上的外观。

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:state_focused="true"
            android:drawable="@drawable/btn_check_on_focused_holo_light" /> 
    <item android:state_checked="false" android:state_focused="true"
            android:drawable="@drawable/btn_check_off_focused_holo_light" />
    <item android:state_checked="false"
            android:drawable="@drawable/btn_check_off_holo_light" />
    <item android:state_checked="true"
            android:drawable="@drawable/btn_check_on_holo_light" />
</selector>

【讨论】:

  • 要在所有设备和所有版本中保持一致的外观和感觉,您必须如上所述对其进行自定义。
  • 这似乎是最好的解决方案,尤其是考虑到@CRSardar 的评论。谢谢。
【解决方案2】:

我最近遇到了同样的问题并找到了真正的解决方案,而不仅仅是一种解决方法。

当您在getView() 方法中扩充您的视图时:

public View getView(int position, View convertView, ViewGroup parent) { return inflater.inflate(R.layout.list_item, parent, false); }

您需要确保您使用的充气器包含 Activity 上下文而不是 Application 上下文,因为 Activity 上下文知道您使用的主题,而 Application 不知道。

因此,请确保您以下列方式之一或类似方式创建充气机:

  • LayoutInflater.from(activity);
  • getLayoutInflater(); // from within activity
  • LayoutInflater.from(parent.getContext());

为了快速检查这个解决方案,只需替换:

return inflater.inflate(R.layout.list_item, parent, false);

与:

return LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);

【讨论】:

    【解决方案3】:

    我也有这个问题。我正在使用三星 Galaxy S4,我的应用程序中的 CheckBox 显示为老式类型。如果我在“Android Emulator”中运行相同的应用程序,它看起来就像我想要的那样。

    我建议的最好的事情,不要担心。只需在市场上上传应用程序就可以了。

    【讨论】:

      【解决方案4】:

      尝试将 AndroidManifest.xml 文件中的 targetSdkVersion 设置为最新版本(确保已下载该 sdk 版本):

      android:targetSdkVersion="19"
      

      【讨论】:

      • 这实际上不可能是问题的根源,因为他在两个不同的设备上尝试了具有相同清单的相同应用程序..
      猜你喜欢
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      相关资源
      最近更新 更多