安卓课程十七 Button图文混排的按钮
16lz
2021-01-23
使用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 {@Overrideprotected 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);}@Overridepublic 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;}}
更多相关文章
- Android 代码模拟输入按键
- Android 获取手势onfling代码片段
- Android 设置对话框中的确定按钮的 enabled 属性
- android —— 常用代码utils
- Android中打开扬声器关闭麦克风的代码实现
- Android Studio:在xml布局中不能自动补全代码