实现功能:图文混排的按钮(丰富按钮的效果)
在功能上也没有增加太多,难度也不大。
但已经开始能读懂代码的逻辑,并修改bug了。
功能效果:
java代码:
package com.exam.android_textview3;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)this.findViewById(R.id.button);//通过id找控件
SpannableString spannableStringLeft = new SpannableString("left");
Bitmap bitmapLeft = BitmapFactory.decodeResource(getResources(), R.drawable.left);
ImageSpan imageSpanLeft = new ImageSpan(bitmapLeft,DynamicDrawableSpan.ALIGN_BOTTOM);
spannableStringLeft.setSpan(imageSpanLeft, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableString spannableStringRight = new SpannableString("right");
Bitmap bitmapRight = BitmapFactory.decodeResource(getResources(), R.drawable.right);
ImageSpan imageSpanRight = new ImageSpan(bitmapRight);
spannableStringRight.setSpan(imageSpanRight,0,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
button.append(spannableStringLeft);
button.append("我的按钮");
button.append(spannableStringRight);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/first"
android:text="按钮1" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/medimum"
android:drawablePadding="30dp"
android:text="qianqian" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/first"
android:text="按钮3" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="30dp"
android:drawableRight="@drawable/first"
android:text="按钮4" >
</Button>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp" >
</Button>
</LinearLayout>
总结:
1.通过id找控件: Button button = (Button)this.findViewById(R.id.button);
2.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,这是在 setSpan 时需要指定的 flag。
用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果。分别有 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)、Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)、Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)、Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)
3.注意LinearLayout的嵌套循环。