安卓课程十七 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; } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28