[Original] How to dynamically set drawableLeft and so on for TextView in Android, androidtextview
How to dynamically set drawableLeft for TextView in AndroidTwo methods: Method 1: manually set inherent boundaries
1 Drawable drawable = getResources (). getDrawable (resId); 2 // check the TextView method. setCompoundDrawables (Drawable, Drawable) 3 // note that the Set drawable must have passed Drawable. the setBounds method sets the boundary parameter 4 //. Therefore, this row is required to be 5 drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); 6 setCompoundDrawables (drawable, null, null );
Method 2: Automatically Set inherent boundaries
1 setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);2 //setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(resId), null, null, null);
When the drawableLeft attribute is used in android TextView, this is to display the image in the text, but involves multiple lines of text
You can use the following method to achieve the desired effect:
You can use the AbsoluteLayout absolute layout for the text and Image Layout. The image is placed in the ImageView control, and the multi-line text is placed in the LargeText control, because it is an absolute layout, the widget is not moved. You can try it.
Android: dynamically add TextView in LinearLayout. Several Questions
Article 1:
You can define text in xml. For example, the textview layout file is defined as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<TextView xmlns: android = "schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: textSize = "@ dimen/textsize"
Android: ellipsize = "end"
Android: layout_height = "@ dimen/height_22">
</TextView>
In the code, you can use inflater to convert the xml into a view, which is roughly written as follows:
TextView text1 = (TextView) inflater. inflate (this, R. layout. text, null );
Article 2:
There are more implementation methods for occupying one row of two texts,
For example, you can put two textviews in xml in a horizontal linearLayout, or use RelativeLayout to put them in the same row. The key is to control both textviews without line breaks.
Article 3:
There are also several ways to achieve the split line.
For example, two textview controls have an imageview control, and imageview uses a line image.
You can also use the drawableLeft drawableRight attribute of textview to set the image to the drawableRight of the previous textview or to the drawableLeft of the next textview. You can either use an image or define it yourself.