【问题标题】:Custom TextView XML Drawable with background property具有背景属性的自定义 TextView XML Drawable
【发布时间】:2015-07-10 09:26:24
【问题描述】:

我正在使用 Horizo​​ntal ListView(列表仅包含 TextView),现在我想使用 xml drawable 在列表项下方显示 line,无论 user taps

正如我们通常在Sliding Tabs 中使用的那样,请查看下面的屏幕截图,在此示例中,用户选择了 Tab1Tab2Tab3处于正常状态。

以同样的方式,我想实现我的custom drawable 以与我的TextView 获得相同的效果,为此,我关注this 链接。

但我仍然没有成功,请查看我的 TextView 的属性:

<TextView
    android:text="@string/app_name"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"        
    android:layout_margin="5dp"
    android:textColor="#000000"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:background="@drawable/custom_textview"
    android:ellipsize="marquee" />

custom_textview.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <layer-list>
            <item>
                <shape>
                    <solid android:color="#ffff7a00" />
                </shape>
            </item>
            <item android:bottom="3dp">
                <shape>
                    <solid android:color="#222222" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

【问题讨论】:

  • 您正在使用哪些类型的标签?设计库还是 TabHost 还是 TabLayout?
  • 兄弟我没有使用任何标签?再次阅读我的问题
  • 哦,对不起,我明白了...
  • 尝试在您的选择器 xml 中也添加 项。

标签: android xml textview drawable


【解决方案1】:

用此代码替换您的 custom_textview.xml

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ffff7a00" />
        </shape>
    </item>

    <item
        android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#222222" />
        </shape>
    </item>
</layer-list>

【讨论】:

  • 但我得到了每一个列表项,而我只想显示选定的一个,例如:- 上方屏幕中的 Tab1
  • 好吧,尝试为TextView设置属性android:textIsSelectable="true"。
  • 尝试为 TextView 设置此属性 android:textIsSelectable="true" 并在 java 中当您选择此 TextView 时尝试设置 textView.setSelected(true);它的工作..
【解决方案2】:

您可以使用简单的逻辑来做到这一点 创建row.xml

<LinearLayoutadd attributes with orientation vertical>
<TextView/>
<View android:id="@+id/checkId/>
</LinearLayout/>

创建模型(POJO)

public class MyModel{
String name;
boolean isChecked;
public MyModel(String name,boolean isChecked){
this.name=name;
this.isChecked = isChecked;
}
//apply getters and setters
}

现在创建你的适配器 在您的 getView 方法中 检查值 isChecked 是 true 还是 false 如果为 true 设置 id 为 checkId 的视图的背景颜色 否则将其更改为白色

现在转到您的 OnItemClickListener 使用 for 循环确保单击了哪个项目

for(int i = 0 ;i < youlist.size;i++){
yourlist.get(i).setIsChecked(false);
}
yourlist.get(positionoflistitem).setIsChecked(true);
youradapter.notifyDataSetChanged();

你完成了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 2014-10-24
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    相关资源
    最近更新 更多