我有一个imageView中使用OnClickListener,这种方式我有一个自定义按钮。Android:自定义按钮与图像,而文本对齐到左侧和右侧
我也有文字旁边的imageView,但我希望他们能够点击附近的文字以及。
我想知道是否有可能创建一个可绘制文件,只有一个按钮。
这是我到目前为止有: http://i.imgur.com/OnKN56i.png
我有一个imageView中使用OnClickListener,这种方式我有一个自定义按钮。Android:自定义按钮与图像,而文本对齐到左侧和右侧
我也有文字旁边的imageView,但我希望他们能够点击附近的文字以及。
我想知道是否有可能创建一个可绘制文件,只有一个按钮。
这是我到目前为止有: http://i.imgur.com/OnKN56i.png
您可以用常规的按钮做到这一点。 使用drawableLeft
和drawableRight
属性设置图像。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:drawableLeft="@drawable/ic_chevron_left_white_24dp"
android:text="Back" />
<Button
android:id="@+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:drawableRight="@drawable/ic_chevron_right_white_24dp"
android:text="Next" />
</RelativeLayout>
您还可以设置android:background="@null"
删除的按钮轮廓,仅显示文本和图标。它会看起来更接近你的屏幕截图,用户可以点击图标或文本,按钮将获得点击。
创建一个CustomView:
package com.android4dev.navigationview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
/**
* Created by 77930 on 2016/1/24.
*/
public class MyView extends ImageView {
public interface OnAreaClickListener{
public void onLeftClick(MyView view);
public void onRightClick(MyView view);
}
public OnAreaClickListener listener;
public OnAreaClickListener getListener() {
return listener;
}
public void setListener(OnAreaClickListener listener) {
this.listener = listener;
}
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if(event.getX() < getWidth()/3){
if(listener!=null) listener.onLeftClick(MyView.this);
}else if(event.getX() > getWidth()/3){
if(listener!=null) listener.onRightClick(MyView.this);
}
}
return super.onTouchEvent(event);
}
}
添加布局:
<com.android4dev.navigationview.MyView
android:id="@+id/btn_all"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
绑定点击:
MyView myView = (MyView)findViewById(R.id.btn_all);
myView.setListener(new MyView.OnAreaClickListener() {
@Override
public void onLeftClick(MyView view) {
}
@Override
public void onRightClick(MyView view) {
}
});
好了,这样的形象工程。尽管背景“@null”无效,但我使用了透明颜色。现在由于某种原因,图像太左而且文字不显示。 我确实将高度和宽度设置为50dp。 [链接](http://i.imgur.com/rB2oxgt.png) – Axiom
同样通过设置宽度为“wrap_content”。图像变得非常大。此外,文字离实际图像很远。另外,我设法通过将每个按钮的填充设置为-15dp将按钮更靠近边缘。尽管如此,图像非常大。 – Axiom
[updated_image](http://i.imgur.com/mJbt7Oo.png) – Axiom