2016-01-24 127 views

回答

0

您可以用常規的按鈕做到這一點。 使用drawableLeftdrawableRight屬性設置圖像。

<?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"刪除的按鈕輪廓,僅顯示文本和圖標。它會看起來更接近你的屏幕截圖,用戶可以點擊圖標或文本,按鈕將獲得點擊。

+0

好了,這樣的形象工程。儘管背景「@null」無效,但我使用了透明顏色。現在由於某種原因,圖像太左而且文字不顯示。 我確實將高度和寬度設置爲50dp。 [鏈接](http://i.imgur.com/rB2oxgt.png) – Axiom

+0

同樣通過設置寬度爲「wrap_content」。圖像變得非常大。此外,文字離實際圖像很遠。另外,我設法通過將每個按鈕的填充設置爲-15dp將按鈕更靠近邊緣。儘管如此,圖像非常大。 – Axiom

+0

[updated_image](http://i.imgur.com/mJbt7Oo.png) – Axiom

0

創建一個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) { 

     } 
    }); 
相關問題