【问题标题】:Navigating throughout screens android develpment通过屏幕导航 android 开发
【发布时间】:2014-02-21 17:54:35
【问题描述】:

您好,只是想知道是否有人可以帮助我,我是 JAVA(android 开发编程)的新手,做过一些 HTML 和 C#,仅此而已。我最近 决定创建一个应用程序,但不知道如何浏览屏幕、主屏幕到练习等等。大约有30个屏幕 (页面)在应用程序中,每个描述一个练习,包括图像。我想知道完成这项工作的最佳方法是什么? 我创建了 2 个 XML 文件,如下所示。

Activity_main.xml

    <LinearLayout 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"
        android:background="#808080"
        android:orientation="vertical"
        tools:context=".MainActivity" >

       <TextView
            android:id="@+id/title"
            android:layout_width="184dp"
            android:layout_height="65dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="0dp"
            android:gravity="center"
            android:text="@string/description"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#FFFF00"
            android:textSize="26sp" />

        <ImageView
            android:id="@+id/exercise"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="40dp"
            android:src="@drawable/exercises"
            android:clickable="true"
            android:onClick="myClickHandler" />

        <ImageView
            android:id="@+id/routines"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="22dp"
            android:src="@drawable/routines" />

        <ImageView
            android:id="@+id/logbook"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="22dp"
            android:src="@drawable/logbook" />

        <ImageView
            android:id="@+id/qrscanner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="22dp"
            android:src="@drawable/qrscanner" />

        <TextView
            android:id="@+id/Copyright"
            android:layout_width="206dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="220dp"
            android:layout_marginTop="70dp"
            android:text="@string/copyright"
            android:textColor="#FFFF00"
            android:textSize="14sp" />


        <ImageView
            android:id="@+id/Logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="230dp"
            android:layout_marginTop="-440dp"
            android:src="@drawable/logov2" />

    </LinearLayout>

也是一个练习.xml

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="452dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/Logo"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginLeft="230dp"
        android:layout_marginTop="0dp"
        android:src="@drawable/logov2" />

     <ImageView
         android:id="@+id/chest"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="12dp"
         android:layout_marginTop="30dp"
         android:src="@drawable/chest_exercises" />

     <ImageView
        android:id="@+id/shoulder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="-82dp"
        android:src="@drawable/shoulderexercises" />

         <ImageView
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="230dp"
        android:layout_marginTop="-82dp"
        android:src="@drawable/backexercises" />

        <ImageView
        android:id="@+id/bicep"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/bicepexercises" />

        <ImageView
        android:id="@+id/tricep"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="-82dp"
        android:src="@drawable/tricepexercises" />


       <ImageView
        android:id="@+id/abbs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="230dp"
        android:layout_marginTop="-82dp"
        android:src="@drawable/abbsexercises" />

        <ImageView
        android:id="@+id/quad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/quadexercise" />

        <ImageView
        android:id="@+id/calves"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="-82dp"
        android:src="@drawable/calvesexercises" />

         <ImageView
        android:id="@+id/glutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="230dp"
        android:layout_marginTop="-82dp"
        android:src="@drawable/gluteexercises" />

        <ImageView
        android:id="@+id/hamstring"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/hamstringexercises" />


        <ImageView
        android:id="@+id/traps"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="-82dp"
        android:src="@drawable/trapexercises" />

        <TextView
        android:id="@+id/title"
        android:layout_width="184dp"
        android:layout_height="65dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="-470dp"
        android:gravity="center"
        android:text="@string/musclegroup"
        android:textColor="#FFFF00"
        android:textSize="26sp" />
    </LinearLayout>
</ScrollView>

我只是想知道如何从主屏幕导航到exercises.xml? 如果我使用 imageviews 作为按钮可以吗?

提前致谢 瑞恩

【问题讨论】:

    标签: java android navigation


    【解决方案1】:

    我建议使用按钮并将图像设置为背景。在识别触摸时,图像有时会很糟糕。
    所以导航将是例如主屏幕 -> 点击锻炼图片 -> 点击身体部位 -> 查看锻炼?
    有了这么多项目,图像/按钮就完全可以了,但是如果您的子菜单变大,您应该开始考虑使用 ListViews 来提高性能。
    如果你有这样的层次结构,你可以为每个新屏幕做一个新的活动,或者使用片段。当您通过屏幕时,片段会更流畅,但如果有清晰的层次结构,我建议从活动开始。
    活动不允许您保持不变的选项卡栏,因为您将真正交换整个屏幕,但如果没有元素您希望在每个屏幕上保持相同,这不应该是一个问题。

    在我看来,Fragment 是一种更好的方式来处理几乎所有事情。问题是,对于初学者来说,片段要复杂得多。因此,如果您有时间,请继续深入研究 Fragments,但如果您赶时间,只需为每个屏幕调用一个 Activity 即可。

    【讨论】:

    • 是的,你的导航很成功,这就是它的工作方式。我从来没有使用过碎片,这很难吗?我最近尝试了按钮的 onclicklistener 方法,但我根本无法让它工作!你知道任何简单但有效的教程吗?谢谢
    • 想象片段的最佳方式是混合活动和视图。它们几乎和活动一样强大,但只能显示在底层活动的一部分上。他们之间的过渡非常顺利。您将在活动中添加一个片段。当在那个 Fragment 中完成时,你会做一个回调,也许是在一个 onClick 中,它会在你的 Activity 中调用一个函数。在那里,您可以用下一个 Fragment 替换 Fragment。我发现 Fragments 的概念非常困难,但一旦知道它们非常易于使用并且看起来很棒。
    • 对于教程,我还推荐 mykong 教程,它们非常好,因为它们只解释了您需要的内容。 mkyong.com/android/android-button-example 对于 Fragments,此线程中有一些不错的:stackoverflow.com/questions/5710573/need-a-fragments-example
    【解决方案2】:

    通常您想要做的是使用 Fragments(我建议)或新的 Activity 来显示不同的 xml 文件。

    要从http://developer.android.com/guide/components/fragments.html 复制一些代码,这是执行此操作的首选方式。

    FragmentManager fragmentManager = getFragmentManager()
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ExerciseFragment fragment = new ExerciseFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
    

    使用 ImageViews 作为按钮是完全有效的,只要它对用户来说是显而易见的。您的处理程序代码可能如下所示:

    public void myClickHandler(View view){
        int id=view.getId();
        if(id==R.id.exercises){          
            doSomething()
        }else if(id=R.id.logbook){
        }....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多