编辑:我的问题不同,因为引用的答案不是试图访问和修改drawable(这是我的问题所在),但它只是替换了drawable。
我试图在按钮内的布局中更改drawable中的形状颜色。
我的问题是:如何以编程方式访问shape元素的id?
这是包含选择器内形状的drawable(它被称为' ic_mf_account_icon.xml'):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/accountShape">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/btn_bg_normal" />
<corners android:radius="@dimen/programmatic_btn_corner_radius" />
</shape>
</item>
<item android:right="10dp" android:left="10dp" android:top="10dp" android:bottom="10dp" android:drawable="@drawable/ic_server"/>
</layer-list>
</item>
</selector>
这是包含我刚刚列出的drawable的布局(drawable在顶部的Button中引用):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/FilesListItem" >
<FrameLayout style="@style/FilesListContent" >
<Button
android:id="@+id/icon"
android:layout_width="36dip"
android:layout_height="36dip"
android:background="@drawable/ic_mf_account_icon" />
<include layout="@layout/files_list_item_overlay" />
</FrameLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingLeft="10dip"
android:singleLine="true" />
</LinearLayout>
<ImageButton
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:background="@null"
android:padding="@dimen/info_icon_padding"
android:src="@drawable/ic_info_grey_normal" />
</LinearLayout>
这是我试图访问drawable的地方。我可以访问初始布局,但是当我尝试从那里开始并访问drawable我得到一个空引用 - 要注意,这是在片段&#39; onCreateView(...)&#39;功能如果重要:
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_account_list, container, false);
FindView find = FindView.inView(view);
// AbsListView absListView = find.absList(android.R.id.list);
AbsListView absListView = view.findViewById(android.R.id.list);
absListView.setAdapter(arrayAdapter = new BaseListAdapter<Account>(getActivity(), accounts) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.fragment_account_list_item, parent, false);
View layoutView = convertView.findViewById(R.id.icon);
View drawableView = layoutView.findViewById(R.id.accountShape);
Drawable drawableBackground = drawableView.getBackground();
// From here I will endeavor to modify the drawables shape background.
以下是我刚才提到的代码中的代码,我试图从布局中访问drawable:
View layoutView = convertView.findViewById(R.id.icon);
View drawableView = layoutView.findViewById(R.id.accountShape);
Drawable drawableBackground = drawableView.getBackground();