English 中文(简体)
重新使用第XML编外法和守则
原标题:Reusing layout XML and the code behind
  • 时间:2010-07-08 18:10:06
  •  标签:
  • android

我正试图将一个 couple子变成一个可再生的安永部分。 我已成功地放弃了XML/ID部分的工作,但我可以指出,如何在各项活动之间重新适用这一框架,而没有在任何地方加以纠正。 我是新来的,因此我很抱歉,如果这一点显而易见的话。

我已多次审查了这一员额:,但似乎缺少几个档案,我没有重建这些档案的足够经验。

我的主要布局:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <WebView android:id="@+id/webview" 
        android:layout_width="fill_parent"
        android:layout_height="375px" 
        android:layout_alignParentTop="true" />

        <include layout="@layout/navbar"/>

</RelativeLayout>

我的“共同”:

<?xml version="1.0" encoding="UTF-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
      <LinearLayout   
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center">

            <ImageButton android:id="@+id/Button1" 
                android:layout_width="71px"
                android:layout_height="wrap_content"
                android:background="@drawable/button1"              
                android:layout_alignParentBottom="true" />


            <ImageButton android:id="@+id/Button2" 
                android:layout_width="75px"
                android:layout_height="wrap_content"
                android:background="@drawable/button1"  
                android:layout_toRightOf = "@+id/Button1"
                android:layout_alignParentBottom="true" />              

        </LinearLayout>
        </merge>

如果你在我的XML上有任何额外的信仰,我也希望这样做。

最佳回答

我确实是包含所有共同守则和直接使用该守则的实际组成部分。 此处为构成部分类别下调:

public class NavigationBar extends LinearLayout {
    public NavigationBar(Context context) {
        super(context);
        setupView(context);
        hookupButtons(context);
    }
    public NavigationBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupView(context);
        hookupButtons(context);
    }
    private void setupView(Context context) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // inflate whatever layout xml has your common xml
        inflater.inflate(R.layout.navigation_bar, this);
    }
}

在我的班子里,我确实认为这样做了。

接着,我在所有的布局中都看到:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.dragonglobal.dragonplayer.ui.widgets.NavigationBar
        android:id="@+id/nav_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ListView
        android:id="@+id/list_of_playlists"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

在每项活动的开展中,我也有这样的情况(你可以适应你们需要):

NavigationBar navbar = (NavigationBar) findViewById(R.id.nav_bar);
navbar.setText("Playlists");

当然,你需要增加你们所需要的进口。

http://www.ohchr.org。

仅作澄清: 我的航海——巴、克斯姆勒与你的航海非常相似,但我没有一丝一线。

<EDIT 2

这里所看的是Buttons。 我只想一顿,但你应该这样做。

private void hookupButtons(final Context context) {
    ImageButton playlistsBtn = (ImageButton)findViewById(R.id.nav_playlists_btn);
    if (context instanceof PlaylistsActivity) {
        playlistsBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.nav_playlists_active));
    } else {
        playlistsBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, PlaylistsActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                context.startActivity(i);
            }
        });
    }
}
问题回答

在XML布局中,你只重新制定可重新使用的布局,而不是采用任何 Java法。

如果你想要再利用 Java逻辑的话,你必须建立一个子级的观点。 从您的链接中抽出的样本非常适合你们的需要:

public class OkCancelBar extends LinearLayout {
...




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签