android开发中关于include的问题,各位帮忙分析一下!
我在一个a.xml中用了一下TabHost,在tab1的标签下include了一个b.xml,在java代码中有一个AActivity.java对a.xml进行操作,还有一个BActivity对b.xml进行操作。怎样才能在a.xml中的tab1下对b.xml的操作分开,在AActivity.java、BActivity这两个java文件中进行操作,我试了如下的方法:即在AActivity.java代码中用startActivity(new Intent (A.this, B.class) ),这样是可以实现跳转,可是只是b.xml这个布局页面,并不在a.xml布局页面的tab1标签下,也就是说我要在a.xml页面下对include进来的b.xml布局文件在BActivity里进行操作,而不是直接跳到b.xml这个页面。我现在用的方法就是在AActivity内对b.xml布局页面进行的操作,不知道各位能不能帮我想想看看能不能把操作给分开?
a.xml代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3" >
</GridView>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/file_sd" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/file_wlyp" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
b.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3" >
</GridView>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
这是我现在对上面的xml文件操作写的代码:
package com.zhenglingkun.myfilemanage;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
public class FileActivity extends Activity {
GridView gridView;
private final int NUMCOLMNS = 3;
// 声明TabHost对象
TabHost fileTabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file);
fileTabHost = (TabHost) this.findViewById(android.R.id.tabhost);
gridView = (GridView) this.findViewById(R.id.gridView1);
fileTabHost.setup();
gridView.setNumColumns(NUMCOLMNS);
fileTabHost.addTab(fileTabHost.newTabSpec("tab_test1")
.setIndicator("TAB 1").setContent(R.id.tab1));
fileTabHost.addTab(fileTabHost.newTabSpec("tab_test2")
.setIndicator("TAB 2").setContent(R.id.tab2));
fileTabHost.addTab(fileTabHost.newTabSpec("tab_test3")
.setIndicator("TAB 3").setContent(R.id.tab3));
fileTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
fileTabHost.setCurrentTab(0);
fileTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Toast.makeText(fileTabHost.getContext(), tabId,
Toast.LENGTH_SHORT).show();
if (tabId.equals("tab_test1")) {
}
}
});
gridView.setAdapter(new MyAdapter(fileTabHost.getContext()));
}
class MyAdapter extends BaseAdapter {
private Integer[] images = { R.drawable.file_manager_category_photo,
R.drawable.file_manager_category_audio,
R.drawable.file_manager_category_video,
R.drawable.file_manager_category_document,
R.drawable.file_manager_category_zip,
R.drawable.file_manager_category_apk,
R.drawable.file_manager_category_image,
R.drawable.file_manager_category_favorite,
R.drawable.file_manager_category_ad };
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(45, 45));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(images[position]);
return imageView;
}
}
}