Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标
16lz
2021-01-23
Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标
注:(图中每一个条目和图标都是由代码动态生成)
代码动态布局,并需要为每一个条目设置图标,此时用到了android:drawableLeft="@drawable/icon"
父xml文件:
[html] view plain copy print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/background">
- <!--子布局由代码动态生成-->
- <LinearLayout
- android:id="@+id/layout_CONTENT"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="@dimen/content_padding">
- <LinearLayout
- android:id="@+id/activity_service_select"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/table_margin"
- android:background="@color/table_background"
- android:orientation="vertical"
- android:padding="@dimen/table_padding">
- </LinearLayout>
- </LinearLayout>
- </ScrollView>
子xml文件:
[html] view plain copy print ?
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="@dimen/row_height"
- android:layout_marginBottom="@dimen/row_margin"
- android:background="@drawable/row_selector"
- android:paddingLeft="@dimen/row_padding_left"
- android:paddingRight="@dimen/row_padding_right">
- <TextView
- android:id="@+id/tv_select_item"
- style="@style/text_18"
- android:layout_width="match_parent"
- android:layout_height="@dimen/row_height"
- android:layout_marginBottom="@dimen/row_margin"
- android:background="@drawable/row_selector"
- android:gravity="center_vertical"
- android:textColor="@drawable/row_text_selector"/>
- <ImageView
- android:id="@+id/iv_icon"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_alignParentRight="true"
- android:duplicateParentState="true"
- android:gravity="center_vertical"
- android:src="@drawable/go"/>
- </RelativeLayout>
代码中引用:
[java] view plain copy print ?
- privateViewGroupmLayout;
- privateintimg[]={R.drawable.zikao1,R.drawable.zikao2,R.drawable.zikao3,R.drawable.zikao4};
- /*(non-Javadoc)
- *@seeapp.ui.TitleActivity#onCreate(android.os.Bundle)
- */
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setUpViews();
- }
- privatevoidsetUpViews()
- {
- setContentView(R.layout.activity_service_select);
- setTitle(R.string.text_select);
- showBackwardView(R.string.button_backward,true);
- mLayout=(ViewGroup)findViewById(R.id.activity_service_select);
- finalString[]mSelfSelect=getResources().getStringArray(R.array.text_self_select);
- //需要布局的行数
- finalintrowCount=mSelfSelect.length;
- for(inti=0;i<rowCount;i++){
- finalLinearLayoutlinearLayout=newLinearLayout(this);
- View.inflate(this,R.layout.service_examvaluable_item,linearLayout);
- finalViewview=linearLayout.getChildAt(0);
- view.setTag(i+1);
- view.setOnClickListener(this);
- Drawabledrawable=getResources().getDrawable(img[i]);
- drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
- finalTextViewmTextView=(TextView)linearLayout.findViewById(R.id.tv_select_item);
- mTextView.setCompoundDrawables(drawable,null,null,null);//设置TextView的drawableleft
- mTextView.setCompoundDrawablePadding(10);//设置图片和text之间的间距
- mTextView.setText(mSelfSelect[i]);
- //添加到屏幕布局
- LayoutParamslayoutParams=newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
- mLayout.addView(linearLayout,layoutParams);
- }
- }
在程序中直接取出子xml中TextView中的id,并动态设置改变了 DrawableLeft。
解决方案:
[java] view plain copy- publicvoidsetCompoundDrawables(Drawableleft,Drawabletop,Drawableright,Drawablebottom);
类似调用方法如下:
1.在XML中使用
[java] view plain copy- android:drawableLeft="@drawable/icon"
2.代码中动态变化
- Drawabledrawable=getResources().getDrawable(R.drawable.drawable);
- drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
- myTextview.setCompoundDrawables(drawable,null,null,null);
参考另一个函数:
[java] view plain copy- publicvoidsetCompoundDrawablesWithIntrinsicBounds(Drawableleft,
- Drawabletop,Drawableright,Drawablebottom)
更多相关文章
- Android Studio使用Lint进行代码检查
- 初学Android做计时器和代码
- Android UI 开发入门—线性布局
- android 源代码构建和运行cts
- 实现TextView的垂直滚动以及通过代码设置TextView滚动无法显示滚
- Android Studio使用XML样式在JAVA代码中的使用(使用java代码调节x
- Android下使用dlopen函数动态调用.so链接库 [转]
- Android中apk动态加载技术研究(1)基础知识研修