重用布局XML和背后的代码

reu*_*cam 15 android

我试图将几个按钮变成Android中的可重用组件.我已经成功地使XML/UI部分工作,但我无法弄清楚如何使代码背后的代码可以在活动之间重用,而不是在任何地方重新编码.我是Android的新手,所以如果这很明显我会道歉.

我已经多次审阅过这篇文章:Android布局技巧3 - 第1部分,但似乎缺少一些文件,而且我没有足够的经验来重建它们.

我的主要布局的一个愚蠢的版本:

  <?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>
Run Code Online (Sandbox Code Playgroud)

然后我的"组件":

<?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>
Run Code Online (Sandbox Code Playgroud)

如果您对我的XML有任何其他批评,我也会很感激.

Jer*_*nes 16

我所做的是创建一个包含所有公共代码的实际组件并直接使用它.这是组件类的一个愚蠢的版本:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的班级中,hookupButtons完全符合你的想法.:-)

然后我的所有布局xmls看起来都类似于:

<?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>
Run Code Online (Sandbox Code Playgroud)

在每个活动的onCreate中我也有这个(你可以适应你需要的任何东西):

NavigationBar navbar = (NavigationBar) findViewById(R.id.nav_bar);
navbar.setText("Playlists");
Run Code Online (Sandbox Code Playgroud)

当然,您需要添加所需的任何导入.

编辑

只是澄清一下:我的navigation_bar.xml看起来与你的非常相似,只是我没有合并行.

编辑2

这是hookupButtons的样子.我只显示一个按钮,但你应该明白这个想法.

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);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)