转载自:https://www.cnblogs.com/alimjan/p/7102001.html
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">
<Button
android:id="@+id/btn_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:drawableLeft="@mipmap/ic_launcher"
android:drawablePadding="5dp"
android:text="热烈欢迎"
android:textColor="#000000"
android:textSize="17sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在左"
android:textColor="#000000"
android:textSize="15sp" />
<Button
android:id="@+id/btn_top"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在上"
android:textColor="#000000"
android:textSize="15sp" />
<Button
android:id="@+id/btn_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在右"
android:textColor="#000000"
android:textSize="15sp" />
<Button
android:id="@+id/btn_bottom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在下"
android:textColor="#000000"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
java:
package com.example.alimjan.hello_world;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class class__2_3_4 extends AppCompatActivity implements View.OnClickListener {
private Button btn_icon;
private Drawable drawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_2_3_4);
btn_icon = (Button) findViewById(R.id.btn_icon);
Button btn_left = (Button) findViewById(R.id.btn_left);
Button btn_top = (Button) findViewById(R.id.btn_top);
Button btn_right = (Button) findViewById(R.id.btn_right);
Button btn_bottom = (Button) findViewById(R.id.btn_bottom);
btn_left.setOnClickListener(this);
btn_top.setOnClickListener(this);
btn_right.setOnClickListener(this);
btn_bottom.setOnClickListener(this);
drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), // 必须设置图片大小,否则不显示图片
drawable.getMinimumHeight());
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_left) {
btn_icon.setCompoundDrawables(drawable, null, null, null);
} else if (v.getId() == R.id.btn_top) {
btn_icon.setCompoundDrawables(null, drawable, null, null);
} else if (v.getId() == R.id.btn_right) {
btn_icon.setCompoundDrawables(null, null, drawable, null);
} else if (v.getId() == R.id.btn_bottom) {
btn_icon.setCompoundDrawables(null, null, null, drawable);
}
}
public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class__2_3_4.class);
mContext.startActivity(intent);
}
}