适用于 Android 的自定义 Tabwiget [英] Custom Tabwiget for Android
问题描述
我设计了一个用于选项卡(自定义全局活动)的活动,此活动有不同的按钮,如选项卡和单击按钮调用相应的活动,所以如果我正在调用 A(假设)活动,然后调用B(假设)活动并返回 A,在这种情况下,再次创建活动.我希望此活动的行为类似于 tabwidget
并从 onResume()
开始.是这可能与否,如果是,那么请给我建议.谢谢
I have designed an activity which I'm using for tab(Custom Global activity),this activity has different buttons like tab and clicking on buttons calling corresponding activity so if I'm calling A(suppose) activity and as then call B(suppose)activity and coming back on A,in this case A activity is created again.I want this activity should behave like tabwidget
and start from onResume()
.Is this possible or not if yes then how please suggest me.Thanks
全局标签布局
<LinearLayout 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"
android:background="#1a1a1a"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="@+id/liveTV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:padding="5dp"
android:src="@drawable/tab_livetv_selector" />
<ImageView
android:id="@+id/movies"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:padding="5dp"
android:src="@drawable/tab_movie_selector" />
<ImageView
android:id="@+id/vod"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:padding="5dp"
android:src="@drawable/tab_vod_selector" />
<ImageView
android:id="@+id/events"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:padding="5dp"
android:src="@drawable/tab_event_selector" />
<ImageView
android:id="@+id/playlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:padding="5dp"
android:src="@drawable/tab_playlist_selector" />
</LinearLayout>
全局选项卡的 Java 代码
public class Header extends LinearLayout implements OnClickListener {
private Context mContext;
private ImageView liveTV;
private ImageView movies;
private ImageView vod;
private ImageView events;
private ImageView playlist;
public static String tab = null;
public static boolean destroy = false;
public Header(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.header, this, true);
liveTV = (ImageView) findViewById(R.id.liveTV);
movies = (ImageView) findViewById(R.id.movies);
vod = (ImageView) findViewById(R.id.vod);
events = (ImageView) findViewById(R.id.events);
playlist = (ImageView) findViewById(R.id.playlist);
liveTV.setOnClickListener(this);
movies.setOnClickListener(this);
vod.setOnClickListener(this);
events.setOnClickListener(this);
playlist.setOnClickListener(this);
}
public void init() {
// setting selector for selected tab
if (tab.equals("movies")) {
destroy = true;
movies.setSelected(true);
liveTV.setSelected(false);
vod.setSelected(false);
events.setSelected(false);
playlist.setSelected(false);
} else if (tab.equals("vod")) {
destroy = true;
vod.setSelected(true);
liveTV.setSelected(false);
movies.setSelected(false);
events.setSelected(false);
playlist.setSelected(false);
} else if (tab.equals("events")) {
destroy = true;
events.setSelected(true);
liveTV.setSelected(false);
movies.setSelected(false);
vod.setSelected(false);
playlist.setSelected(false);
} else if (tab.equals("playlist")) {
destroy = true;
playlist.setSelected(true);
liveTV.setSelected(false);
movies.setSelected(false);
vod.setSelected(false);
events.setSelected(false);
} else {
destroy = true;
liveTV.setSelected(true);
movies.setSelected(false);
vod.setSelected(false);
events.setSelected(false);
playlist.setSelected(false);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//have to put here code for click
}
}
}
然后必须像另一个视图一样将以下代码放入 xml 中
then have to put only following code in xml like another view
<com.media.ui.Header
android:id="@+id/layoutHeader"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom" />
推荐答案
在一个 TabActivity 中添加多个活动的代码,请写下面的代码,它会解决你的问题.
Please write below code instead of your code for add multiple activities in one TabActivity, it will solve your problem.
ActivityStack.java
ActivityStack.java
private Stack<String> stack;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (stack == null)
stack = new Stack<String>();
// start default activity
push("FirstStackActivity", new Intent(this, Tab_SampleActivity.class));
}
@Override
public void finishFromChild(Activity child) {
pop();
}
@Override
public void onBackPressed() {
pop();
}
public void push(String id, Intent intent) {
Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
stack.push(id);
setContentView(window.getDecorView());
}
}
public void pop() {
if (stack.size() == 1)
finish();
LocalActivityManager manager = getLocalActivityManager();
manager.destroyActivity(stack.pop(), true);
if (stack.size() > 0) {
Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
Window newWindow = manager.startActivity(stack.peek(), lastIntent);
setContentView(newWindow.getDecorView());
}
}
TabActivity.java
TabActivity.java
public class TabActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_screen);
TabHost tabHost = getTabHost();
Intent intent = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.home));
spec.setContent(intent);
tabHost.addTab(spec);
Intent intent1 = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec1 = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.invoice));
spec1.setContent(intent1);
tabHost.addTab(spec1);
tabHost.setCurrentTab(0);
}
}
FirstActivity.java
FirstActivity.java
public class FirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Tab Sample Activity ");
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getParent(), SecondActivity.class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("SecondActivity", intent);
}
});
setContentView(textView);
}
}
SecondActivity.java
SecondActivity.java
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("First Stack Activity ");
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getParent(), ThirdActivity.class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("ThirdActivity", intent);
}
});
setContentView(textView);
}
}
ThirdActivity.java
ThirdActivity.java
public class ThirdActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
将以下 XML 文件添加到您的 res/layout 文件夹中.
Add Below XML files into your res/layout folder.
1) tab_screen.xml
1) tab_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="3dp" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
2) main.xml
2) main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
AndroidManifest.xml:-
AndroidManifest.xml:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.tabsample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".FirstActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TabActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityStack"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ThirdActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
有关在一个 TabActivity 下添加多个活动的详细信息,请参阅下面的链接,并提供完整示例.
And see below link for more information on add multiple activities under one TabActivity with complete example.
Android - 一个 TabActivity 下的多个 Android 活动
这篇关于适用于 Android 的自定义 Tabwiget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!