安卓。SetCompoundDrawables无法工作

3
我正在尝试向TextView(位于列表视图的标题中)设置正确的Drawable。我已经设置好边界,但似乎没有起作用。使用setDrawablesWithInstrinctBounds()方法则可以正常工作。
以下是代码:
private View getHeaderView() {
    final LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.legislation_article_list_header, null, false);
    final TextView textView = (TextView) view.findViewById(R.id.sEdition);
    headerSpinner = textView;

    final Drawable d = getActivity().getResources().getDrawable(R.drawable.spinner_arrow_down);
    d.setBounds(new Rect(0, 0, headerSpinner.getHeight(), headerSpinner.getHeight()));
    headerSpinner.setCompoundDrawables(null, null, d, null);

    textView.setOnClickListener(new View.OnClickListener() {

       .............

        }
    });
    return view;
}

什么是问题?

使用 setCompoundDrawablesWithIntrinsicBounds(null, null, d, null); - Phantômaxx
为了增加填充,您可以在xml中使用android:drawablePadding="8dp",或者在Java中使用setCompoundDrawablePadding(12) - Phantômaxx
1个回答

12

试一试:

Drawable d = getActivity().getResources().getDrawable(R.drawable.spinner_arrow_down);
d.setBounds(new Rect(0, 0, headerSpinner.getHeight(), headerSpinner.getHeight()));
textView.setCompoundDrawablesWithIntrinsicBounds( d , null, null, null);
或者
textView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接