【问题标题】:In Android, how to pass data from class to corresponding layout/fragment file?在Android中,如何将数据从类传递到相应的布局/片段文件?
【发布时间】:2014-09-18 06:43:48
【问题描述】:

背景:我正在编写一个 Android 应用程序,主要是按照 the official developer guides 的说明进行操作。我有一些编写 Java 代码的经验,但很少使用 xml 和 Android。

问题:我想将静态类“PlaceholderFragment”(包含在“BoardContainer”类中)中变量的信息传递给我的片段布局文件“fragment_board.xml”。 PlaceholderFragment 看起来像这样(在 Eclipse 为我创建后大部分未编辑):

public static class PlaceholderFragment extends Fragment {

     public int nButtons = 2;
     public static class PlaceholderFragment extends Fragment {

         private int nButtons = 2;

         public PlaceholderFragment() {
         }

         @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_board,
                container, false);
         return rootView;
         }
     }

(其他生命周期回调尚未实现)。

现在我的 fragment_board.xml 是这样的:

<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"
    tools:context="com.example.exampletest.MainGame$PlaceholderFragment" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="49dp"
            android:layout_height="49dp"
            android:contentDescription="@null"
            android:onClick="buttonPressed" //not yet implemented
            android:src="@drawable/grid2" />

</RelativeLayout>

这里我想使用int实例变量nButtons,例如,如果nButtons==7,那么我们得到android:src="@drawable/grid7而不是grid2,或者布局文件将包含七个ImageButtons而不仅仅是一个,等等。也就是说,如何让xml文件从其对应的类中读取并理解实例变量?

【问题讨论】:

    标签: java android xml android-layout android-fragments


    【解决方案1】:

    很遗憾,XML 文件不能read and understand the variables from its corresponding class。相反,我们可以通过在类文件中获取包含在 XML 文件中的组件的句柄并像这样更改它们来以编程方式更改它们:

    ImageButton imgHandle = (ImageButton) findViewById(R.id.imageButton1);
    
    if(nButtons == 7) {
        imgHandle.setImageResource(R.id.grid7);
    }
    

    在片段中,您需要在 onCreateView 方法中使用 rootView:

    ImageButton imgHandle = (ImageButton)rootView.findViewById(R.id.imageButton1);
    

    【讨论】:

    • 太好了,我得到了这个工作,虽然 xml 文件无法理解相应类中的变量,但很遗憾。例如,假设我在类中有一个布尔变量,如果它是真的,我的片段文件中会有一个特定的 ImageButton,否则没有。是否可以这样做,或者我只需要拥有一堆不同的片段 xml 文件?
    • 处理这个问题的一种方法是将 ImageButton 放在 xml 文件中,如果你决定不让它显示,你可以在你的类代码中获取对它的引用并使用 setVisibility 函数GONE 常量,因此它不会出现
    • 感谢您的帮助。我试图了解如何实现一个复杂的布局系统,该系统会根据用户输入而变化。例如:一个有关卡的游戏,每个关卡使用不同大小的棋盘和不同的棋子。将其变成应用程序的专业方法是什么 - 为每个级别使用不同的布局文件,或者使用您在此处建议的方法?如果我选择后一个选项,我需要解决这个问题:如何根据相应类文件中的实例变量在我的 GridLayout 中设置“app:columnCount”?
    • 我还没有在 Android 上开发过游戏,所以我可能不是提供建议的最佳人选,请随时在 SO 上提出另一个问题,我相信有人更有资格将能够提供帮助。
    • 这是我的后续问题:stackoverflow.com/questions/24976986/…
    猜你喜欢
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    相关资源
    最近更新 更多