android 带文字的ImageButton有两种实现方法

如下:

实现一:

<LinearLayout

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical"

android:id="@+id/bt">

<ImageView

android:id="@+id/ib"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ringlove"

android:background="#00000000"

/>

<TextView

android:id="@+id/tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="ssss"

/>

</LinearLayout>

package com.oschina.test;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnTouchListener;

import android.widget.LinearLayout;

import android.widget.Toast;

public class MainActivity extends Activity {

LinearLayout m_ll;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

m_ll=(LinearLayout)findViewById(R.id.bt);

m_ll.setClickable(true);

m_ll.setOnClickListener(ocl);

m_ll.setOnTouchListener(otl);

}

public OnClickListener ocl=new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show();

}

};

public OnTouchListener otl=new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

// TODO Auto-generated method stub

if(event.getAction()==MotionEvent.ACTION_DOWN)

{

m_ll.setBackgroundColor(Color.rgb(127,127,127));

}

else if(event.getAction()==MotionEvent.ACTION_UP)

{

m_ll.setBackgroundColor(Color.TRANSPARENT);

}

return false;

}

};

}

实现二:

<FrameLayout

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

<ImageButton

android:id="@+id/yugao"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/btn_yugao"

android:background="#00000000"

android:layout_gravity="center"

/>

<TextView

android:id="@+id/yugao_label"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="aaaaaa"

android:textColor="@color/blue"

/>

</FrameLayout>

更多相关文章

  1. 把TextView中的文字添加阴影效果及Style的使用
  2. Android控制文字水平间距android:letterSpacing
  3. Android Studio导入第三方类库的方法
  4. Android TextView 文字居中
  5. android 的常标签和方法 android 初学者
  6. Android 实现显示文字的Gallery
  7. Android给TextView添加点击事件的实现方法
  8. 赢得争论的方法只有一个,那就是避免争论
  9. 详细介绍MvcPager分页控件使用的实例方法

随机推荐

  1. Android(安卓)SDK下载技巧
  2. android TextView属性大全
  3. Android中widget组件的开发流程
  4. Android使用service后台更新计划任务
  5. android播放器(music player)源码分析1-Ser
  6. 布局概述之相对布局RelativeLayout
  7. Android的Task和Activity(一)
  8. Android高手应该精通哪些内容?
  9. Android开发艺术探索——第二章:IPC机制(上
  10. Android实现退出时关闭所有Activity的方