0

我只做了两个活动,这些是 sherlockActivity。

我可以按标签。但是,并没有改变任何东西。

以下是我的来源。

对不起。并欣赏。

MainActivity.java

package com.example.sherlocktest;

imports;

public class MainActivity extends SherlockActivity implements ActionBar.TabListener {

private ActionBar aBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cal);

    /** TabBar */
    aBar = getSupportActionBar();
    aBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    aBar.setTitle("Darphin's bar");

    ActionBar.Tab tab1 = aBar.newTab().setText("버튼1").setTabListener(this);
    ActionBar.Tab tab2 = aBar.newTab().setText("버튼2").setTabListener(this);
    ActionBar.Tab tab3 = aBar.newTab().setText("버튼3").setTabListener(this);

    aBar.addTab(tab1);
    aBar.addTab(tab2);
    aBar.addTab(tab3);
}

@Override
public void onTabSelected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
    if(tab.equals(aBar.getTabAt(0))) {
        Toast.makeText(this,"1",Toast.LENGTH_SHORT).show();
//          setContentView(R.layout.activity_cal);
    }
    else if(tab.equals(aBar.getTabAt(1))) {
        Toast.makeText(this,"2",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MainActivity.this , ListActivity.class);
        startActivity(intent);
//          setContentView(R.layout.activity_list);
    }
    else if(tab.equals(aBar.getTabAt(2))) {
        Toast.makeText(this,"3",Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onTabUnselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}

@Override
public void onTabReselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
}

ListActivity.java

包 com.example.sherlocktest;

imports

public class ListActivity extends SherlockActivity implements ActionBar.TabListener {

private ActionBar aBar;
private ListView listView;
private List arrayList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    /** TabBar */
    aBar = getSupportActionBar();
    aBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    aBar.setTitle("Darphin's bar");

    ActionBar.Tab tab1 = aBar.newTab().setText("버튼1").setTabListener(this);
    ActionBar.Tab tab2 = aBar.newTab().setText("버튼2").setTabListener(this);
    ActionBar.Tab tab3 = aBar.newTab().setText("버튼3").setTabListener(this);

    aBar.addTab(tab1);
    aBar.addTab(tab2);
    aBar.addTab(tab3);

    /** ListView */
    arrayList = new ArrayList<String>();

    arrayList.add("first");

    listView = new ListView(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list, arrayList);
    listView.setAdapter(adapter);
}

@Override
public void onTabSelected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
    if(tab.equals(aBar.getTabAt(0))) {
        Toast.makeText(this,"1",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(ListActivity.this , MainActivity.class);
        startActivity(intent);
    }
    else if(tab.equals(aBar.getTabAt(1))) {
        Toast.makeText(this,"2",Toast.LENGTH_SHORT).show();
//          setContentView(R.layout.activity_list);
    }
    else if(tab.equals(aBar.getTabAt(2))) {
        Toast.makeText(this,"3",Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onTabUnselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}

@Override
public void onTabReselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
}

activity_cal.xml

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<CalendarView
    android:id="@+id/calendar"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

活动列表.xml

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ListActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >

</ListView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/listView1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="28dp"
    android:text="TextView" />

</RelativeLayout>

sherlockTestManifest.xml

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ListActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >

</ListView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/listView1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="28dp"
    android:text="TextView" />

</RelativeLayout>
4

1 回答 1

0

考虑使用appcompat-v7library 而不是Sherlock,它包含大部分功能。

if(tab.equals(aBar.getTabAt(0))) {
      Toast.makeText(this,"1",Toast.LENGTH_SHORT).show();
//          setContentView(R.layout.activity_cal);
}
else if(tab.equals(aBar.getTabAt(1))
...

看起来这种比较不起作用。尝试为您的标签分配标签

还,

arrayList = new ArrayList<String>();

arrayList.add("first");

listView = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list, arrayList);
listView.setAdapter(adapter);

您正在创建新的 ListView,为什么?你甚至不把它放在任何地方。你应该找到一个现有的 ListView findViewById

于 2013-10-30T10:13:00.830 回答