今天宠物迷的小编给各位宠物饲养爱好者分享代码设置drawable的宠物知识,其中也会对如何在代码中动态为TextView设置drawableRight(android动态添加textview)进行专业的解释,如果能碰巧解决你现在面临的宠物相关问题,别忘了关注本站哦,现在我们开始吧!
1 <TextView android:id="@+id/Title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawableRight="@drawable/check_down" android:gravity="center_vertical" android:textSize="24dip" android:maxLines="1" android:ellipsize="end"/> 我们写在xml的时候,都是这么写的。那代码呢?我们发现TextView他有一个方法 2 public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom); 这个方法呢,就是可以在Java代码动态的画 左上右下几个方向 类似于xml中的 android:drawableLeft="@drawable/icon" android:drawableTop="@drawable/icon" android:drawableRight="@drawable/icon" android:drawableButtom="@drawable/icon" 3 具体在代码中的用法是: Drawable drawable = getResources().getDrawable(R.drawable.spinner_checked); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界 titleTv.setCompoundDrawables(null, null, drawable, null);//画在右边 4 运行之后的效果 如何在代码中动态为TextView设置drawableRight 如何在代码中动态为TextView设置drawableRight 5 界面比较丑,勿喷,大家也还可以通过这个方法实现 public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom) 转载
1
<TextView
android:id="@+id/Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="@drawable/check_down"
android:gravity="center_vertical"
android:textSize="24dip"
android:maxLines="1"
android:ellipsize="end"/>
我们写在xml的时候,都是这么写的。那代码呢?我们发现TextView他有一个方法
2
public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom);
这个方法呢,就是可以在Java代码动态的画 左上右下几个方向
类似于xml中的 android:drawableLeft="@drawable/icon"
android:drawableTop="@drawable/icon"
android:drawableRight="@drawable/icon"
android:drawableButtom="@drawable/icon"
3
具体在代码中的用法是:
Drawable drawable = getResources().getDrawable(R.drawable.spinner_checked);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
titleTv.setCompoundDrawables(null, null, drawable, null);//画在右边
4
运行之后的效果
如何在代码中动态为TextView设置drawableRight
如何在代码中动态为TextView设置drawableRight
5
界面比较丑,勿喷,大家也还可以通过这个方法实现
public void setCompoundDrawablesWithIntrinsicBounds (Drawable left,
Drawable top, Drawable right, Drawable bottom)
1 <TextView android:id="@+id/Title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawableRight="@drawable/check_down" android:gravity="center_vertical" android:textSize="24dip" android:maxLines="1" android:ellipsize="end"/> 我们写在xml的时候,都是这么写的。那代码呢?我们发现TextView他有一个方法 2 public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom); 这个方法呢,就是可以在Java代码动态的画 左上右下几个方向 类似于xml中的 android:drawableLeft="@drawable/icon" android:drawableTop="@drawable/icon" android:drawableRight="@drawable/icon" android:drawableButtom="@drawable/icon" 3 具体在代码中的用法是: Drawable drawable = getResources().getDrawable(R.drawable.spinner_checked); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界 titleTv.setCompoundDrawables(null, null, drawable, null);//画在右边 4 运行之后的效果 如何在代码中动态为TextView设置drawableRight 如何在代码中动态为TextView设置drawableRight 5 界面比较丑,勿喷,大家也还可以通过这个方法实现 public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)
<TextView
android:id="@+id/Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="@drawable/check_down"
android:gravity="center_vertical"
android:textSize="24dip"
android:maxLines="1"
android:ellipsize="end"/>
我们写在xml的时候,都是这么写的。那代码呢?我们发现TextView他有一个方法
类选择器:类选择器允许以一种**于文档元素的方式来指定样式。 该选择器可以单独使用,也可以与其他元素结合使用。 使用以下语法向这些归类的元素应用样式,即类名前有一个点号(.),然后结合通配选择器
Android 自带图标库 android.R.drawable desandroidstylejavahtmlsrc文件Gocom 在xml文件中调用。 android:title="@string/secure_connect" android:orderInCategory="100" android:icon="@android:drawable/ic_menu_search" android:drawableRight="@android:drawable/arrow_down_float"/> 程序中调用。 setIcon(android.R.drawable.stat_notify_error) Android 自带图标库 android.R.drawable
你是说View占满了整个屏幕吗?这样的话就只能在layout中写一个布局文件,将View以一种组件的方式声明到.xml文件中,然后你可以在它的上下中声明其他的button或者textview或者ImageButton等。
1、创建imageview对象 2、设置imageview的图片 3、添加到布局中 示例代码 ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup); //获取原来的布局容器ImageView imageView = new ImageView(this); //创建imageviewimageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); //image的布局方式imageView.setImageResource(R.drawable.ic_launcher); //设置imageview呈现的图片group.addView(imageView); //添加到布局容器中,显示图片。
可以的,selector里面包含
。
然后把这个selector加入 xml中的drawableLeft属性上就行了。
或者代码中设置editText.setCompoundDrawables();getDrawable(R.drawable.selector)作为参数传进去就行了
本文由宠物迷 百科常识栏目发布,非常欢迎各位朋友分享到个人朋友圈,但转载请说明文章出处“如何在代码中动态为TextView设置drawableRight”