qq_26687333 2015-05-22 09:01 采纳率: 87.5%
浏览 1599
已采纳

做侧滑布局错误,我找不到怎解决,大牛们帮看看,白菜求解

package com.test.cehua.sindleMenu;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class SindleMenu extends HorizontalScrollView {

private LinearLayout mWapper;
private ViewGroup mMenu;
private ViewGroup mContent;
private int screenWidth;// 屏幕的宽度
// 单位是dp
private int mMenuRightPadding = 50;// 菜单距离屏幕右侧的宽度

private boolean once = false;

private int mMenuWidth;// 定义菜单区的宽度

/**
 * 未用自定义菜单的时候,调用
 * 
 * @param context
 * @param attrs
 */
public SindleMenu(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    /*
     * 获取到屏幕的宽度
     */
    //隐藏标题栏


    WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);// 获取到window服务

    DisplayMetrics outMetrics = new DisplayMetrics();
    // 通过window服务获取到量度值outMetrics
    windowManager.getDefaultDisplay().getMetrics(outMetrics);
    screenWidth = outMetrics.widthPixels;// 通过量度值获取到对应的频宽像数值
    /**
     * 把mMenuRightPadding(菜单距离屏幕右侧的宽度)的单位转换成像数值 第一个参数是单位转换 第一个参数是要转换成多少
     */
    // 把dp转换成px
    mMenuRightPadding = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources()
                    .getDisplayMetrics());

}

/**
 * 设置子view的宽和高 设置自己的宽和高
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (!once) {
        // 获取界面的布局集
        mWapper = (LinearLayout) getChildAt(0);
        // 获取到我们的mMenu菜单 元素
        mMenu = (ViewGroup) mWapper.getChildAt(0);
        // 获取到布局文件第二个元素 就是我们的显示布局
        mContent = (ViewGroup) mWapper.getChildAt(1);
        // 设置mMenu左边菜单栏的宽度, 就等于屏幕宽度 - 我们设置的mMenuRightPadding(菜单距离屏幕右侧的宽度)
        mMenuWidth = mMenu.getLayoutParams().width = screenWidth
                - mMenuRightPadding;
        // 只是Mcontent的宽度
        mContent.getLayoutParams().width = screenWidth;
        // mWapper.getLayoutParams().width =
        // screenWidth+mMenu.getLayoutParams().width ;
        once = true;
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

/**
 * 把mMenu和mContent设置的显示布局中 通过使用偏移量把mMenu隐藏
 */

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

    if (changed) {
        // 设置显示 这是把mMenu隐藏到左侧
        this.scrollTo(mMenuWidth, 0);
    }
    super.onLayout(changed, l, t, r, b);
}

/**
 * 判断鼠标/光标是否在操作,按下移动之类的
 */

@Override
public boolean onTouchEvent(MotionEvent ev) {
    //
    int action = ev.getAction();
    switch (action) {
    // 在炒作手指抬起时
    case MotionEvent.ACTION_UP:
        // 得到隐藏在左边的宽度值
        int s =getScrollX();

        if (s >= mMenuWidth / 2) {
            //我们就隐藏mMenu
            //smoothScrollTo用这个方法有个缓慢的动画效果ScrollTo是瞬间隐示的

            this.smoothScrollTo(mMenuWidth, 0);
        }else{
            //当隐藏部分的宽度大于mMenu的一般时我们就显示mMenu菜单
            //把隐藏的宽度设置为0就可以了
            this.smoothScrollTo(0, 0);
        }
        return true;

    }
    return super.onTouchEvent(ev);
}

}


<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img_frame_background"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/img_1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/imageView1"
            android:text="第1个菜单项"
            android:textColor="#ffffff"
            android:textSize="26sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/img_2" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/imageView2"
            android:text="第2个菜单项"
            android:textColor="#ffffff"
            android:textSize="26sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/img_3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/imageView3"
            android:text="第3个菜单项"
            android:textColor="#ffffff"
            android:textSize="26sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/img_4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/imageView4"
            android:text="第4个菜单项"
            android:textColor="#ffffff"
            android:textSize="26sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/img_5" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/imageView5"
            android:text="第5个菜单项"
            android:textColor="#ffffff"
            android:textSize="26sp" />
    </RelativeLayout>
</LinearLayout>


主菜单页面


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

<com.test.cehua.sindleMenu.SindleMenu
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <include layout="@layout/lift_menu" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/qq" >
    </LinearLayout>
</com.test.cehua.sindleMenu.SindleMenu>

  • 写回答

2条回答 默认 最新

  • 王小红_Sixteen 2015-05-25 01:17
    关注

    放上错误信息和源代码才能帮你看哦

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突