日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

可自由配置的图文混排控件——组合法

發布時間:2024/3/26 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 可自由配置的图文混排控件——组合法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、我們希望控件可以這樣定制:

<com.itemp.imagetext.ImageText

android:layout_width="wrap_content" android:layout_height="wrap_content" talent:image_src="@mipmap/weather" talent:image_width="50dp" talent:image_height="50dp" talent:image_position="left" talent:text_size="15sp" talent:text_color="#f00" /> PS:當然,不要忘記自定義命名空間的引入 xmlns:talent="http://schemas.android.com/apk/res-auto"
2、為了實現上述美好設想,我們引入自定義的屬性,位于res/values/attrs.xml中(如果你喜歡也可以叫fuck.xml) <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ImageText"><attr name="image_src" format="reference"/><attr name="image_width" format="dimension"/><attr name="image_height" format="dimension"/><attr name="text_size" format="dimension"/><attr name="text_color" format="color"/> <!--枚舉型自定義屬性--> <attr name="image_position" format="enum"> <enum name="left" value="0"/> <enum name="right" value="1"/> <enum name="top" value="2"/> <enum name="bottom" value="3"/> </attr> </declare-styleable></resources>3、令所有構造方法都去間接調用三個參數的構造方法public ImageText(Context context) {this(context,null); }public ImageText(Context context, AttributeSet attrs) {this(context, attrs,0); }
4、在三個參數的構造方法中完成對自定義屬性的讀取public ImageText(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);//從界面中拿到屬性的值 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageText); // float imageWidth = typedArray.getDimension(R.styleable.ImageText_image_width, 50); //TypedValue.COMPLEX_UNIT_DIP=dimension的單位,50=dimension的默認值 int imageWidth = (int) typedArray.getDimension(R.styleable.ImageText_image_width, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));int imageHeight = (int) typedArray.getDimension(R.styleable.ImageText_image_height, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));Drawable drawable = typedArray.getDrawable(R.styleable.ImageText_image_src);int imagePosition = typedArray.getInt(R.styleable.ImageText_image_position, 0);float textSize = typedArray.getDimension(R.styleable.ImageText_text_size, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));int textColor = typedArray.getColor(R.styleable.ImageText_text_color, Color.BLACK);typedArray.recycle();Log.e(TAG, "ImageText:iw/dr/ip/tc="+imageWidth+"/"+drawable+"/"+imagePosition+"/"+textColor); //接下來根據不同的用戶配置引入不同的布局}
5、根據不同的用戶配置引入不同的布局(在布局中實現控件的“組合”)if(imagePosition==0){//將布局文件的丟到實例本身的肚子里(將“劉德華的樣子”裝入“自己的空皮囊”),將來貼到界面上給用戶看 //最后一個參數,true=加載出來的View連同root一起返回給用戶看 view = LayoutInflater.from(context).inflate(R.layout.widget_imagetext_left, this, true); }else {view = LayoutInflater.from(context).inflate(R.layout.widget_imagetext_right, this, true); }//找到子控件 iv = ((ImageView) view.findViewById(R.id.iv)); tv = ((TextView) view.findViewById(R.id.tv));//為控件設置值 iv.setLayoutParams(new LayoutParams(imageWidth,imageHeight)); iv.setImageDrawable(drawable); tv.setTextSize(textSize); tv.setTextColor(textColor);

OVER!


總結

以上是生活随笔為你收集整理的可自由配置的图文混排控件——组合法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。