【问题标题】:Android - Dynamic Slide MenuAndroid - 动态幻灯片菜单
【发布时间】:2013-12-31 05:22:43
【问题描述】:

使用tutorial(源代码here),我能够在android中为菜单和主要内容使用单一的xml布局创建一个幻灯片菜单。

问题是我需要使菜单和内容动态化。我可以创建一个动态视图,但我不知道如何使用按钮\滑动切换到第二个动态视图,或者创建两个视图并在按下按钮时滑出一个。内容必须是动态的,菜单将基于来自互联网的文件。我查阅了许多教程,但我还没有完全找到如何将所有教程融合到一个连贯的项目中。

所以这意味着要么从上面链接的源代码中使视图菜单和视图上下文动态化(而不是从 xml 加载它),或者简单地创建两个我可以根据按钮切换移动的视图。

这是非动态版本:

MainActivity.java

package com.example.slider;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.example.slider.view.viewgroup.FlyOutContainer;


public class MainActivity extends Activity{

FlyOutContainer root;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_sample, null);

    this.setContentView(root);

    }
@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

public void toggleMenu(View v){
    this.root.toggleMenu();
}


}

这是 FlyOutContainer.java

package com.example.slider.view.viewgroup;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class FlyOutContainer extends LinearLayout {


private View menu;
private View context;

protected static final int menuMargin = 125;

public enum MenuState{
    Closed, Open
};

protected int currentContentOffset = 0;
protected MenuState menuCurrentState = MenuState.Closed;

public FlyOutContainer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub

}

public FlyOutContainer(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public FlyOutContainer(Context context) {
    super(context);
    // TODO Auto-generated constructor stub 
}

protected void onAttachedToWindow(){
    super.onAttachedToWindow();





    this.menu = this.getChildAt(0);

    this.context = this.getChildAt(1);



    this.menu.setVisibility(View.GONE);     
}

protected void onLayout(boolean changed, int left, int top, int right, int bottom){
    if(changed)
        this.calculateChildDimensions();

        this.menu.layout(left, top, right - menuMargin, bottom);

        this.context.layout(left + this.currentContentOffset, top, right
                + currentContentOffset, bottom);

}

public void toggleMenu(){
    switch(this.menuCurrentState){
    case Closed:
        this.menu.setVisibility(View.VISIBLE);
        this.currentContentOffset = this.getMenuWidth();
        this.context.offsetLeftAndRight(currentContentOffset);
        this.menuCurrentState = MenuState.Open;
        break;
    case Open:
        this.context.offsetLeftAndRight(-currentContentOffset);
        this.currentContentOffset = 0;
        this.menuCurrentState = MenuState.Closed;
        this.menu.setVisibility(View.GONE);
        break;
    }
    this.invalidate();
}

private int getMenuWidth(){
    return this.menu.getLayoutParams().width;
}


private void calculateChildDimensions(){
    this.context.getLayoutParams().height = this.getHeight();
    this.context.getLayoutParams().width = this.getWidth();

    this.menu.getLayoutParams().height = this.getHeight();
    this.menu.getLayoutParams().width = this.getWidth() - menuMargin;
}


}

【问题讨论】:

    标签: java android dynamic


    【解决方案1】:

    已经有一些不错的库可供您使用。无需重新发明轮子。

    或者使用官方的:

    【讨论】:

    • 是的,您可以通过 xml 布局为活动(使用容器)设置菜单,或者通过将 xml 布局资源 id 设置为应该膨胀的视图,或者您可以直接设置一个真实的视图对象。也可以设置菜单的宽度,否则会设置默认值
    • 实际上,现在我想知道是否可以通过编程方式编辑strings.xml 或下载新的strings.xml。那也可以。
    • 我不知道你在说什么!
    • NavigationDrawer 菜单是通过 strings.xml 中的数组生成的。我想知道我是否可以在服务器上放置一个 xml 文件,然后让应用程序下载该文件并以此为基础。
    • 我猜你误会了什么。通常显示为菜单的视图是一个 ListView,您为 ListView 提供一个适配器来显示菜单项。这与 stings.xml 文件无关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 2015-05-27
    • 1970-01-01
    • 2015-09-29
    • 2010-10-11
    • 2017-02-18
    相关资源
    最近更新 更多