第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

自定義帶圖片和文字的ImageTextButton

標(biāo)簽:
Android

今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学习。下一次或者过几天我会从自定义属性,在布局文件中使用属性的方式再讲一篇关于自定义控件的文章,希望对大家能够有所帮助。

现在开始讲自定义带图片和文字的ImageTextButton的实现方法。

效果图如下:

5bdeeed900018da609100527.jpg

第一步:新建一个image_text_buttton.xml的布局文件,供自定义的控件使用。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:orientation="vertical" >


    <ImageView

        android:id="@+id/iv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"

        android:layout_marginTop="12dp" />


    <TextView

        android:id="@+id/tv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"

        android:layout_marginTop="8dp"

        android:textColor="#000000" />


</LinearLayout>


第二步:自定义一个类ImageTextButton继承LinearLayout

package net.loonggg.itbd.view;


import net.loonggg.itbd.R;

import android.content.Context;

import android.util.AttributeSet;

import android.view.LayoutInflater;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;


public class ImageTextButton extends LinearLayout {

private ImageView iv;

private TextView tv;


public ImageTextButton(Context context) {

super(context);

}


public ImageTextButton(Context context, AttributeSet attrs) {

super(context, attrs);

LayoutInflater.from(context).inflate(R.layout.image_text_buttton, this,

true);

iv = (ImageView) findViewById(R.id.iv);

tv = (TextView) findViewById(R.id.tv);

}


public void setDefaultImageResource(int resId) {

iv.setImageResource(resId);

}


public void setDefaultTextViewText(String text) {

tv.setText(text);

}


/**

 * @param resId

 */

public void setImageResource(int resId) {

iv.setImageResource(resId);

}


/**

 * @param text

 */

public void setTextViewText(String text) {

tv.setText(text);

}


/**

 * @param color

 */

public void setTextColor(int color) {

tv.setTextColor(color);

}


}


第三步:自定义控件的使用,在布局文件activity_main.xml中的使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >


    <net.loonggg.itbd.view.ImageTextButton

        android:id="@+id/itb"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />


</RelativeLayout>


第四步:在Activiy中的使用

public class MainActivity extends Activity {

private ImageTextButton itb;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


itb = (ImageTextButton) findViewById(R.id.itb);

itb.setImageResource(R.drawable.sure);

itb.setTextViewText("确定");

}


}

原文链接:http://www.apkbus.com/blog-47151-59553.html

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消