解决方案 »
- 怎样获取安卓系统文件中的MP3,和获取MP3的时长
- android程序报The specified child already has a parent错误
- 如何捕获输入框的长按事件
- 如何生成android可用的SO文件
- java.lang.NullPointerException 一定会crash么?
- googleMap如何自己添加坐标?
- 问控件在xml实例化时构造函数能不能带参数?
- 关于android 编程捕获logcat问题
- AndroidStudio开发,使用iconify字体图标IconFontDescriptor类无法使用
- 小白求助,报错Android resource linking failed
- Android客户端,与服务器通信,一个服务器关于连接SQL的问题。
- 关于android下开发聊天软件的方法及设计思想
现在已经是左中右三个按钮,但是中间的如果作为handle的话,就会把两侧的撑出去
<TableRow />
<TableRow />
<TableRow />
</TableLayout>
试试这种布局 ,TableRow依次放你的图片就行
@+id/handle 不能放在整行上面,这样点击或者触摸handle的时候两边的按钮也会受影响。
但是如果放在了中间一列,又会出现我说的问题——左右两列没了!
private int mHandleId = 0; //抽屉行为控件ID
private int[] mTouchableIds = null; //Handle 部分其他控件ID
public int[] getTouchableIds() {
return mTouchableIds;
} public void setTouchableIds(int[] mTouchableIds) {
this.mTouchableIds = mTouchableIds;
} public int getHandleId() {
return mHandleId;
} public void setHandleId(int mHandleId) {
this.mHandleId = mHandleId;
} public MySlidingDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MySlidingDrawer(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}
/*
* 获取控件的屏幕区域
*/
public Rect getRectOnScreen(View view){
Rect rect = new Rect();
int[] location = new int[2];
View parent = view;
if(view.getParent() instanceof View){
parent = (View)view.getParent();
}
parent.getLocationOnScreen(location);
view.getHitRect(rect);
rect.offset(location[0], location[1]);
return rect;
}
public boolean onInterceptTouchEvent(MotionEvent event) {
// 触摸位置转换为屏幕坐标
int[] location = new int[2];
int x = (int)event.getX();
int y = (int)event.getY();
this.getLocationOnScreen(location);
x += location[0];
y += location[1];
// handle部分独立按钮
if(mTouchableIds != null){
for(int id : mTouchableIds){
View view = findViewById(id);
Rect rect = getRectOnScreen(view);
if(rect.contains(x,y)){
Log.i("MySlidingDrawer on touch"
, String.format("Action=%d Button=%s"
, event.getAction()
, ((Button)view).getText().toString()
));
//return
boolean result = view.dispatchTouchEvent(event);
Log.i("MySlidingDrawer dispatchTouchEvent", "" + result);
return false;
}
}
}
// 抽屉行为控件
if(event.getAction() == MotionEvent.ACTION_DOWN && mHandleId != 0){
View view = findViewById(mHandleId);
Log.i("MySlidingDrawer on touch"
, String.format("%d,%d", x, y));
Rect rect = getRectOnScreen(view);
Log.i("MySlidingDrawer handle screen rect"
, String.format("%d,%d %d,%d", rect.left, rect.top, rect.right, rect.bottom));
if(rect.contains(x, y)){//点击抽屉控件时交由系统处理
Log.i("MySlidingDrawer", "Hit handle");
}else{
return false;
}
}
return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("MySlidingDrawer on touch", "Action=" + event.getAction());
return super.onTouchEvent(event);
}}
测试代码:public class SlidingDrawerTest extends Activity{
Button btnLeft, btnRight, btnContent;
MySlidingDrawer ctlSlidingDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.slidingdrawer); ctlSlidingDrawer = (MySlidingDrawer)this.findViewById(R.id.ctlSlidingDrawer);
ctlSlidingDrawer.setHandleId(R.id.ctlHandle);
ctlSlidingDrawer.setTouchableIds(new int[]{R.id.btnLeft, R.id.btnRight});
btnLeft = (Button)this.findViewById(R.id.btnLeft);
btnLeft.setOnClickListener(onLeftClickListener);
btnRight = (Button)this.findViewById(R.id.btnRight);
btnRight.setOnClickListener(onButtonClickListener);
btnContent = (Button)this.findViewById(R.id.btnContent);
btnContent.setOnClickListener(onButtonClickListener);
}
private OnClickListener onLeftClickListener = new OnClickListener(){
public void onClick(View v) {
Log.i("SlidingDrawerTest", "left button click");
}
};
private OnClickListener onButtonClickListener = new OnClickListener(){
public void onClick(View v) {
Button button = (Button)v;
Log.i("SlidingDrawerTest", "Click button " + button.getText().toString());
}
};
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.HelloTest.MySlidingDrawer android:layout_width="wrap_content" android:id="@+id/ctlSlidingDrawer" android:layout_height="match_parent" android:handle="@+id/handle" android:content="@+id/content">
<LinearLayout android:id="@+id/handle" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="Left" android:id="@+id/btnLeft" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Handle" android:id="@+id/ctlHandle"></Button>
<Button android:text="Right" android:id="@+id/btnRight" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="Middle" android:id="@+id/btnContent" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView android:text="This is the content" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</android.HelloTest.MySlidingDrawer>
</LinearLayout>
1.两侧按钮施加点击事件后,单击会执行两次;
2.长按中间的handle区域,会触发两侧按钮的pressed状态,我在这个状态里面为按钮添加了背景图片。分数已送出,再次感谢!
“2.长按中间的handle区域,会触发两侧按钮的pressed状态,我在这个状态里面为按钮添加了背景图片。”
1.去掉在xml文件中对Button定义的点击状态,将android:background更改为一张默认的资源图片;
2.在Activity里对左右两边的Button添加OnTouchListener的事件,在该监听事件中,可以针对左右两边的Button分别设置Down/Move/Up/Cancel的效果。希望对你有帮助。
但是在Miui的系统里面 LeftButton与RightButton点击事件不能执行
请问有解决方法吗?