当前位置: 代码迷 >> Android >> 安卓教程十七 Button图文混排的按钮
  详细解决方案

安卓教程十七 Button图文混排的按钮

热度:59   发布时间:2016-05-01 10:33:53.0
安卓课程十七 Button图文混排的按钮

使用xml配置如下:activity_main.xml

<LinearLayout 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"    tools:context=".MainActivity"     android:orientation="vertical"><LinearLayout     android:layout_width="wrap_content"    android:layout_height="wrap_content"     android:orientation="horizontal">      <Button         android:text="@string/btnImgLeft"        android:layout_width="wrap_content"         android:layout_height="wrap_content"        android:drawableTop="@drawable/star"        android:drawablePadding="1dp"        />    <Button         android:text="@string/btnImgRight"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableRight="@drawable/star"        android:drawablePadding="1dp"        />     <Button         android:text="@string/btnImgTop"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableBottom="@drawable/star"        android:drawablePadding="1dp"        />    <Button         android:text="@string/btnImgBottom"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableLeft="@drawable/star"        android:drawableBottom="@drawable/star"        android:drawablePadding="1dp"        /> </LinearLayout>     <Button         android:id="@+id/btnBig"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /> </LinearLayout>

?java代码实现方式:

package com.example.btncomplext;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.text.SpannableString;import android.text.Spanned;import android.text.style.DynamicDrawableSpan;import android.text.style.ImageSpan;import android.view.Menu;import android.widget.Button;public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);				Button btn = (Button) this.findViewById(R.id.btnBig) ;				String initLeft = "left";		SpannableString spannableLeftString = new SpannableString(initLeft) ;		Bitmap bitmapleft = BitmapFactory.decodeResource(getResources(), R.drawable.star);		ImageSpan imageSpanLeft =new ImageSpan(MainActivity.this,bitmapleft,DynamicDrawableSpan.ALIGN_BOTTOM);		spannableLeftString.setSpan(imageSpanLeft, 0, initLeft.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);				String initRigth = "right";		SpannableString spannableRigthString = new SpannableString(initRigth) ;		Bitmap bitmapright= BitmapFactory.decodeResource(getResources(), R.drawable.star);		ImageSpan imageSpanRight =new ImageSpan(MainActivity.this,bitmapright);		spannableRigthString.setSpan(imageSpanRight, 0, initRigth.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);				btn.append(spannableLeftString);		btn.append("我的按钮");		btn.append(spannableRigthString);	}	@Override	public boolean onCreateOptionsMenu(Menu menu) {		// Inflate the menu; this adds items to the action bar if it is present.		getMenuInflater().inflate(R.menu.main, menu);		return true;	}}

?

  相关解决方案