android自定义组合控件之<ImgView><TextView>

自定义组合控件的界面:boshu.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TableRow>
        <TextView
            android:id="@+id/textview"
            android:textSize="30sp"
            android:text="@string/boshu1"
            />
        <ImageView
            android:id="@+id/bo1"
            android:background="@drawable/greenb"
            android:layout_width="80dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            />

        <ImageView
            android:id="@+id/bo2"
            android:background="@drawable/greyb"
            android:layout_width="80dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            />
        <ImageView
            android:id="@+id/bo3"
            android:background="@drawable/greyb"
            android:layout_width="80dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            />
        <ImageView
            android:id="@+id/bo4"
            android:background="@drawable/greyb"
            android:layout_width="80dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            />
    </TableRow>

</TableLayout>

自定义控件的属性设置,用来对自定义控件中的子控件进行设置attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--自定义控件的属性-->
    <declare-styleable name="boshu">
        <attr name="boshuText" format="string"/>
        <attr name="boshuBg1" format="integer"/>
        <attr name="boshuBg2" format="integer"/>
        <attr name="boshuBg3" format="integer"/>
        <attr name="boshuBg4" format="integer"/>
    </declare-styleable>
</resources>

自定义控件java文件:boshu.java 继承的布局要与boshu.xml定义的布局一致,这里都是TableLayout

public class boshu extends TableLayout {

    private TextView textview;
    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img4;

    private String boshuText;
    private Integer pic1;
    private Integer pic2;
    private Integer pic3;
    private Integer pic4;

    public boshu(Context context, AttributeSet attrs) {
        super(context, attrs);

        //加载视图布局
        LayoutInflater.from(context).inflate(R.layout.boshu,this,true);

        //加载自定义的属性
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.boshu);
        boshuText=a.getString(R.styleable.boshu_boshuText);
        pic1=a.getResourceId(R.styleable.boshu_boshuBg1,R.drawable.greyb);
        pic2=a.getResourceId(R.styleable.boshu_boshuBg2,R.drawable.greyb);
        pic3=a.getResourceId(R.styleable.boshu_boshuBg3,R.drawable.greyb);
        pic4=a.getResourceId(R.styleable.boshu_boshuBg4,R.drawable.greyb);

        //回收资源
        a.recycle();

    }

    //此方法会在所有控件都从xml文件中加载完成后调用,实现“获取子控件对象"功能
    protected void onFinishInflate(){
        super.onFinishInflate();

        //获取子控件
        textview = (TextView)findViewById(R.id.textview);
        img1=(ImageView)findViewById(R.id.bo1);
        img2=(ImageView)findViewById(R.id.bo2);
        img3=(ImageView)findViewById(R.id.bo3);
        img4=(ImageView)findViewById(R.id.bo4);

        //将从资源文件中加载的属性设置给子控件
        if(!TextUtils.isEmpty(boshuText))
        {
            setPageBoshuText(boshuText);
        }
        setPageImage1(pic1);
        setPageImage2(pic2);
        setPageImage3(pic3);
        setPageImage4(pic4);
    }
    public void setPageBoshuText(String text){
        textview.setText(text);
    }
    public void setPageImage1(int resId){
        img1.setBackgroundResource(resId);
    }
    public void setPageImage2(int resId){
        img2.setBackgroundResource(resId);
    }
    public void setPageImage3(int resId){
        img3.setBackgroundResource(resId);
    }
    public void setPageImage4(int resId){
        img4.setBackgroundResource(resId);
    }
}
调用该自定义控件的xml文件:second_main.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.minzu.uidesign.boshu
        android:id="@+id/boshu1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        app:boshuBg1="@drawable/greenb"
        app:boshuBg2="@drawable/greenb"
        app:boshuBg3="@drawable/greyb"
        app:boshuBg4="@drawable/greyb"
        />
/TableLayout>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<!--index.wxml--> <scroll-view class="container" scroll-y="true" scroll-into-view="{{toView}}"  scroll-top="{{scrollTop}}" bindscroll="scroll"> <swiper indicator-dots="{{indicatorDots}}"   autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" class="swiper-box" id="top">   <block wx:for-items="{{banner_list[0].banner}}" wx:key="index">     <swiper-item >       <image class="slide-image" src="{{item.pic_url}}"/>     </swiper-item>   </block> </swiper>  <view>   <view class='menueview' >    <block wx:for-items="{{banner_list[1].banner}}" wx:key="index">      <view  class="menue-1">         <view class="menueimg">             <image src="{{item.pic_url}}" class="menue-image" />         </view>         <view class="menuetext">{{item.title}}</view>     </view>    </block>   </view>  </view> <view class='hg-container' id="new" >   <text class="hg-title">新品推荐</text>         <view class='gooditem'>             <view class='imgview' wx:key="index"  wx:for="{{newgoods}}" wx:for-index="idx" wx:for-item="good">               <image class='productimg' src='/images/icons/1.webp'/>             </view>         </view> </view> <view class='hg-container'>   <text class="hg-title">超值买</text>         <view class='gooditem'>             <view class='imgview' wx:key="index"  wx:for="{{newgoods}}" wx:for-index="idx" wx:for-item="good">               <image class='productimg' src='/images/icons/2.webp'/>             </view>         </view> </view> <view class='hot-container'>   <view class="hot-title">食惠购</view>         <view class='hot-gooditem'>             <view class='hot-imgview' wx:key="index"  wx:for="{{hotgoods}}" wx:for-index="idx" wx:for-item="hot">               <image class='hot-product' src='{{hot.more_pic}}'/>             </view>         </view> </view> <view class="footer" bindtap="tap">   <view>   <text>沪ICP备888888888号</text>   <text>htmlk.cn</text>   <text>微信商城有限公司</text>   <text>北京市xxxxxxxxx</text>   </view> </view> </scroll-view>
06-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值