• Button图文混排的按钮


        按钮上可以设置背景图片,也可以自定义按钮,这里介绍按钮上文字和图片混合排列的效果。

    一、建立工程

    二、activity_main.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="match_parent"
                android:layout_height="120dp"
                android:orientation="horizontal" >
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/star"
                android:text="按钮1"
                />
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/star"
                android:drawablePadding="30dp"
                android:text="按钮2"
                />
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/star"
                android:drawableLeft="@drawable/star"
                android:text="按钮3"
                />
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableRight="@drawable/star"
                android:drawablePadding="30dp"
                android:text="按钮4"
                />
    
        </LinearLayout>
            <Button 
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:id="@+id/button"
                android:layout_marginTop="10dp"
                />
    
    </LinearLayout>
    View Code

    三、MainActivity.java中代码

    package com.study.imagebutton;
    
    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 button = (Button)this.findViewById(R.id.button);
            SpannableString spannableStringLeft = new SpannableString("Left");
            Bitmap bitmapLeft = BitmapFactory.decodeResource(getResources(), R.drawable.image_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.image_right);
            ImageSpan imageSpanRight = new ImageSpan(bitmapRight, DynamicDrawableSpan.ALIGN_BOTTOM);
            spannableStringRight.setSpan(imageSpanRight, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            button.append(spannableStringLeft);
            button.append("我的按钮");
            button.append(spannableStringRight);
            
        }
    
    
        @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;
        }
        
    }
    View Code

    四、效果图

    按钮4的文字本来不是竖着的,是因为控件不够了,所以显示为竖着的形式。

    五、按钮图片

  • 相关阅读:
    Cocos2d-x 学习笔记(15.2) EventDispatcher 事件分发机制 dispatchEvent(event)
    ‎Cocos2d-x 学习笔记(13) ActionEase
    ‎Cocos2d-x 学习笔记(12) Speed Follow
    ‎Cocos2d-x 学习笔记(11.10) Spawn
    ‎Cocos2d-x 学习笔记(11.9) FadeTo FadeIn FadeOut
    ‎Cocos2d-x 学习笔记(11.8) DelayTime ReverseTime TargetedAction ActionFloat Blink TintTo TintBy ResizeTo ResizeBy
    Cocos2d-x 学习笔记(11.7) Repeat RepeatForever
    Cocos2d-x 学习笔记(11.6) Sequence
    指针
    TX2常用命令
  • 原文地址:https://www.cnblogs.com/kingshow123/p/buttonimage.html
Copyright © 2020-2023  润新知