在Android中,如何从类数据传递到相应的布局/片段文件?

问题描述:

背景:我写一个Android应用程序,主要是从以下说明官方的开发者指南。我有编写Java code,但很少使用XML和Android的一些经验。

Background: I am writing an Android app, mostly following instructions from the official developer guides. I have some experience with writing Java code but little with xml and Android.

:我想在我的静态类PlaceholderFragment(这是我的BoardContainer级载)从传递变量的信息到我的片段布局文件fragment_board.xml。 PlaceholderFragment看起来像这样(大多未经编辑的Eclipse创建它为我以后):

Question: I would like to pass information from variables in my static class "PlaceholderFragment" (which is contained by my "BoardContainer" class) to my fragment layout file "fragment_board.xml". PlaceholderFragment looks like this (mostly unedited after Eclipse created it for me):

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;
         }
     }

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

(other lifecycle callbacks have not yet been implemented).

现在我fragment_board.xml是这样的:

Now my fragment_board.xml is like this:

<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 =@绘制/ grid7 而不是 GRID2 ,或者说布局文件将包含七个ImageButtons,而不是只有一个,依此类推。换句话说,如何使XML文件阅读和理解其对应的类的实例变量?

Here I would like to use the int instance variable nButtons so that, for example, if nButtons==7, then we get android:src="@drawable/grid7 instead of grid2, or that the layout file will contain seven ImageButtons instead of just one, and so forth. In other words, how do I make the xml file read and understand the instance variables from its corresponding class?

不幸的是XML文件不能阅读和理解其对应的类的变量。相反,我们可以通过获得一个句柄在类文件中包含的XML文件中的组件和改变他们这样的编程改变它们:

Unfortunately XML files can not read and understand the variables from its corresponding class. Instead we can alter them programatically by obtaining a handle to components contained within XML files in a class file and altering them like this:

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

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

在一个片段,你将需要使用rootView里面你onCreateView方式:

In a fragment you're going to need to use rootView inside of your onCreateView method:

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