【问题标题】:How to place a ">" sign to the far right of a ListView item in Android如何在 Android 中的 ListView 项目的最右侧放置一个“>”符号
【发布时间】:2015-10-15 15:57:34
【问题描述】:

我有一个有 2 个选项的 ListView。我已经用这个代码在一个辅助类中实现了这个:

public void DrawAreas() {
    ListView areaView = (ListView) _activity.findViewById(R.id.area_list);

    ArrayAdapter<TdcArea> areas = new ArrayAdapter<TdcArea>(_activity.getApplicationContext(),
            android.R.layout.simple_list_item_1,
            _system.getAreas()) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView text = (TextView) view.findViewById(android.R.id.text1);
            text.setTextColor(Color.BLACK);
            return view;
        }
    };
    areaView.setAdapter(areas);
    //areaView.setOnItemClickListener()
}

这可行,但是,我想在每个项目的右侧放置一个“>”,表明该项目是可点击的,点击后将显示另一个页面。

如何把那个“>”画到右边?

问候 詹姆

【问题讨论】:

  • 你的意思是在文本中设置一个“>”?除了颜色,我没有看到你在哪里设置任何东西。
  • _system.getAreas() 检索对象数组。每个对象都会覆盖 toString 方法,因此,ListView 实际上会写入一个名为“Name”的属性。如果我在文本中添加“>”,则该符号将显示为未向右对齐,因为所有文本都有不同的长度。
  • 请发布您的 xml 文件。您可以在其中使用 ImageViewalignParent_toRight="true" 并使用可绘制箭头的 src
  • XML 文件不受我控制。它是从外部 WebService 检索并在我的应用程序中创建对象模型。
  • 请注意,您正在尝试执行的操作严格违反了 Android 的设计准则,如开发人员网站 here 中所述。将页面向下滚动大约一半或搜索“不要在订单项上使用右指向插入符”。抱歉没有回答,但我只是想让您了解推荐的指南。

标签: android android-layout android-listview


【解决方案1】:

只需在您的项目中添加一个“chevron right”drawable,然后将其设置到您的 TextView:

// getting drawable as chevron_right
text.setCompoundDrawables(null, null, chevron_right, null);

【讨论】:

  • 你能给我更多的细节吗?我应该创建一个图标并将其放在 res 文件夹中吗?
  • 您可以从这里下载它uxrepo.com/icon/android-chevron-right-by-google-material (png) 并将其复制到您的“drawable”文件夹中
  • 抱歉,我无法让这种方法发挥作用。我从辅助类调用它,但我在构造函数中传递了活动。我被困在试图获取 Drawable 对象。我试过 _activity.getResources().getDrawable(R.drawable.chevrom_red) 但 android studio 说它已被弃用。我尝试了 _activity.getDrawable(R.drawable_chebvron_red) 并出现错误,告诉该调用需要 API 级别 21。请帮助。
  • 谢谢!但我需要使用 setCoumpoundDrawableWithIntrinsicBounds
【解决方案2】:

CHEVRON RIGHT 图标是 Android Studio 图标库中提供的剪贴画资产类型的一部分。步骤如下:

1 - 在 Android Studio 上,右键单击“res”文件夹并选择 New/Vector Asset
2 - 资产类型:剪贴画
3 - 单击“剪贴画”并找到“V形右”
4 - 颜色#CCCCCC
5 - 单击“下一步”,然后单击“完成”
6 - 现在您可以像这样在单元适配器中使用它:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@color/white"
    android:gravity="start|center_vertical"
    android:orientation="horizontal" >


    <TextView
        android:id="@+id/history_details_list_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:maxLines="1"
        android:ellipsize="end"
        android:textColor="#000000"
        android:textSize="11sp" />


    <android.support.v7.widget.AppCompatImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="15dp"
        android:layout_marginEnd="15dp"
        app:srcCompat="@drawable/ic_chevron_right_black_24dp"/>


</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多