1

我有一个 android 应用程序,有一个超类,其中包含每个活动的静态布局,下图以更好的方式显示了这一点,而不是我的描述

在此处输入图像描述

这个“标题”包含一个tabBar包含一个ImageButton。我希望这个标题对于我的应用程序中的所有活动都是静态的。我试图做的是extend从这个超类到我的其他类。超类的代码在下面

public class MySuperClass extends Activity {
 MyHorizontalScrollView scrollView;
 View menu;
 View app;
 ImageButton btnSlide;
 boolean menuOut = false;
 Handler handler = new Handler();
 int btnWidth;

 Button testClass; 

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

    LayoutInflater inflater = LayoutInflater.from(this);
    scrollView = (MyHorizontalScrollView) inflater.inflate(R.layout.horz_scroll_with_list_menu, null);
    setContentView(scrollView);

    menu = inflater.inflate(R.layout.horz_scroll_menu, null);

    app = inflater.inflate(R.layout.horz_scroll_app, null);
    ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);

    ListView listView = (ListView) app.findViewById(R.id.list);

    listView = (ListView) menu.findViewById(R.id.list);

    ArrayList<MenuItem> menuItems = getMenuItems();
    listView.setAdapter(new MenuCustomAdapter(this, menuItems)); 

    btnSlide = (ImageButton) tabBar.findViewById(R.id.BtnSlide);
    btnSlide.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                btnSlide.setImageResource(R.drawable.lincolor);
                break;
            case MotionEvent.ACTION_UP:
                btnSlide.setImageResource(R.drawable.lin);
                break;
            }

            return false;
        }


    });
    btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu));

    testClass = (Button) app.findViewById(R.id.button1);

    testClass.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(MySuperClass.this, TestClass.class);
            startActivity(intent); 
        }


    });

    final View[] children = new View[] { menu, app };

    // Scroll to app (view[1]) when layout finished.
    int scrollToViewIdx = 1;
    scrollView.initViews(children, scrollToViewIdx, new SizeCallbackForMenu(btnSlide));
}

public ArrayList<MenuItem> getMenuItems() {
    ArrayList<MenuItem> items = new ArrayList<MenuItem>();


    MenuItem m1 = new MenuItem(R.drawable.scroll, "Show history"); 
    items.add(m1);
    MenuItem m2 = new MenuItem(R.drawable.right, "Right"); 
    items.add(m2);

    return items;
}


/**
 * Helper for examples with a HSV that should be scrolled by a menu View's width.
 */
static class ClickListenerForScrolling implements OnClickListener {
    HorizontalScrollView scrollView;
    View menu;
    ImageButton button;
    int pressed;
    int timeout;
    /**
     * Menu must NOT be out/shown to start with.
     */
    boolean menuOut = false;

    public ClickListenerForScrolling(HorizontalScrollView scrollView, View menu) {
        super();
        this.scrollView = scrollView;
        this.menu = menu; 
    }



    @Override
    public void onClick(View v) {
        Context context = menu.getContext();

        int menuWidth = menu.getMeasuredWidth();

        // Ensure menu is visible
        menu.setVisibility(View.VISIBLE);

        if (!menuOut) {
            // Scroll to 0 to reveal menu
            int left = 0;
            scrollView.smoothScrollTo(left, 0);
        } else {
            // Scroll to menuWidth so menu isn't on screen.
            int left = menuWidth;
            scrollView.smoothScrollTo(left, 0);
        }
        menuOut = !menuOut;
    }


}

/**
 * Helper that remembers the width of the 'slide' button, so that the 'slide' button remains in view, even when the menu is
 * showing.
 */
static class SizeCallbackForMenu implements SizeCallback {
    int btnWidth;
    View btnSlide;

    public SizeCallbackForMenu(View btnSlide) {
        super();
        this.btnSlide = btnSlide;
    }

    @Override
    public void onGlobalLayout() {
        btnWidth = btnSlide.getMeasuredWidth();
        System.out.println("btnWidth=" + btnWidth);
    }

    @Override
    public void getViewSize(int idx, int w, int h, int[] dims) {
        dims[0] = w;
        dims[1] = h;
        final int menuIdx = 0;
        if (idx == menuIdx) {
            dims[0] = w - btnWidth;
        }
    }
}

}

还有一个测试类,它扩展了这个超类。

public class TestClass extends MySuperClass {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

}

}

再次,我怎样才能tabBar为每个活动制作静态?

4

3 回答 3

1

您可以使用 TabHost 权利。通过单击每个选项卡,您可以启动单独的活动

http://mfarhan133.wordpress.com/2010/11/17/tablayouttabhost-tutorial-for-android-reusing-layout/

于 2012-07-23T08:01:46.913 回答
1

我不知道这是否可能。我将发布一个替代解决方案,它需要更多代码但可以工作,并且是最佳实践,所以如果没有出现,这是最好的替代方案,可能是唯一的一个。

当您一次又一次地尝试使用某个布局时,例如您提到的选项卡栏,布局中有<merge><include>功能。基本思想是制作一个layout.xml要包含在其他布局中的文件。

这篇文章给出了一个很好的例子来说明如何使用它。Android XML 布局中 <merge> 和 <include> 用法的简单示例

于 2012-07-23T08:05:18.037 回答
1

没有办法在活动范围内实现这一点。布局独立于您的 Acitvity (好吧,直到您绑定它们)。

您的问题的解决方案可能是使用通用标题布局,保存在布局文件夹下的 xml 文件中,如下所示:

header.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/red"
    ... />

并使用 include 标签将它们包含在您的布局中:

my_activity_layout.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    ....
    .... >

    <include layout="@layout/header.xml" android:id="@+id/header" />

    <!-- Countinue your layout .... -->

</RelativeLayout>
于 2012-07-23T08:06:17.707 回答