【问题标题】:Is there anything equivalent to setImageResource for buttons?有什么相当于按钮的 setImageResource 的吗?
【发布时间】:2013-06-21 15:43:59
【问题描述】:

我正在创建一个自定义适配器,以便我可以在 listView 中放置一个按钮。我就像放一张图片一样把它放进去,然后我在教程中找到了这行代码:

button.setImageResource(current_item.getButton_id());

我尝试将其更改为setButtonResource,但没有办法。 谢谢你的帮助。 我希望它看起来像这样,其中有一个带有文本视图和按钮的列表视图。我的文本输入得很好,但我不知道如何在列表视图中获取按钮。

这是我的适配器:

private class MyListAdapter extends ArrayAdapter<SchoolsListClass>{
public MyListAdapter() {
    super(searchResults.this, R.layout.da_item, mySchools);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //Make sure we have a view to work with (may have been given null
    View itemView = convertView;
        if (itemView == null){
            itemView = getLayoutInflater().inflate(R.layout.da_item, parent, false);
        }
    //Find school to work with
        SchoolsListClass currentSchoolsListClass = mySchools.get(position);
    //Fill the view
    //school name
    TextView schoolText = (TextView) itemView.findViewById(R.id.item_schoolName);
    schoolText.setText(currentSchoolsListClass.getSchool_name());

    //subscribe/unsubscribe button
    Button button = (Button)itemView.findViewById(R.id.item_subscribeButton);
    button..setImageResource(currentSchoolsListClass.getButton_id());//This is the line of code I am having trouble with. I want it to do exactly what it is doing for images, but use buttons instead of images.
    return itemView;
}

SchoolsListClass:

package com.parse.starter;

public class SchoolsListClass {
private String school_name;
private int button_id;


public SchoolsListClass(String school, int button){
super();
this.school_name = school;
this.button_id = button;



}


public String getSchool_name() {
    return school_name;
    }


    public int getButton_id() {
    return button_id;
        }
}

school_results.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listMain"
    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"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/schoolListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true" >
    </ListView>

</RelativeLayout>

da_item.xml:(我的列表视图中的项目示例)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/itemLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="4dp" >

    <TextView
        android:id="@+id/item_schoolName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/item_subscribeButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/item_subscribeButton"
        android:ellipsize="end"
        android:text="Central Wisconsin Christian School"
        android:textSize="@dimen/school_name_size" />

    <Button
        android:id="@+id/item_subscribeButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

【问题讨论】:

  • 您是说按钮根本没有出现在您的列表视图中,使用此代码?

标签: android button android-listview android-adapter custom-adapter


【解决方案1】:

等价于

button.setBackgroundResource(int resid);

【讨论】:

  • 该方法仅适用于可绘制资源,但我想要一个用于按钮的方法。这就是我想要它做的事情的想法,只是用于按钮而不是图像。
  • 否则,如果您有一个可以与按钮一起使用的适配器,那也可以使用
  • 对不起,我没听明白...你想为你的按钮设置一个图片作为背景吗?
  • 我只想在 listView 中放置一个按钮,但在我遵循的教程中,他们使用了图像,所以 setImageResource 不起作用,因为我在按钮上使用它,那么如何将按钮放入 listView?
  • 设置按钮的背景颜色。例如 button.setBackgroundResource(R.color.RED);
【解决方案2】:

你可以使用,但它在新的 API 中被破坏了

button.setBackgroundDrawable(R.drawable.ic_launcher);

【讨论】:

  • 我正在为 api 级别 8-17 开发,所以我想使用未贬低的方法。
  • 嗨 sunil,setBackgroundDrawable 不只是改变按钮的图片吗?如果是这样,我仍然有问题,因为我需要列表中的每个按钮都具有不同的功能。你有什么建议吗?
猜你喜欢
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 2016-02-08
  • 1970-01-01
相关资源
最近更新 更多