Nabend an alle,
ich wollte mal versuchen, die Actionbar einer App zu verändern, also sprich button einfügen und hintergrundfarbe zu verändern.
Am ende soll es so aussehen:
Wenn man ein neues android-projekt erstellt, kann man ja aussuchen, ob man eine blanke activity oder mit navigation (fixed/scrollable tabs) haben möchte. ich habe fixed tab ausgewählt. dann bräuchte man nur noch oben die actionbartheme leiste zu ändern.
habe dafür eine layout dementsprechend konstruiert:
title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageButton1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/transparent"
android:src="@android:drawable/ic_menu_sort_by_size" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/imageButton2"
android:background="@color/transparent"
android:src="@android:drawable/ic_input_add" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
Alles anzeigen
habe mich nach diesem tutorial gehalten: http://www.youtube.com/watch?v=sRZdP9n9ieM
das heißt, ich hab wie im wiedeo die xml datein erstellt, die anifest angepasst etc. bekam aber ein nullpointerexception.
01-22 17:10:50.954: E/AndroidRuntime(30757): FATAL EXCEPTION: main
01-22 17:10:50.954: E/AndroidRuntime(30757): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asdf/com.example.asdf.MainActivity}: java.lang.NullPointerException
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.app.ActivityThread.access$600(ActivityThread.java:138)
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.os.Looper.loop(Looper.java:137)
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.app.ActivityThread.main(ActivityThread.java:4872)
01-22 17:10:50.954: E/AndroidRuntime(30757): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 17:10:50.954: E/AndroidRuntime(30757): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 17:10:50.954: E/AndroidRuntime(30757): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-22 17:10:50.954: E/AndroidRuntime(30757): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-22 17:10:50.954: E/AndroidRuntime(30757): at dalvik.system.NativeStart.main(Native Method)
01-22 17:10:50.954: E/AndroidRuntime(30757): Caused by: java.lang.NullPointerException
01-22 17:10:50.954: E/AndroidRuntime(30757): at com.example.asdf.MainActivity.onCreate(MainActivity.java:34)
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.app.Activity.performCreate(Activity.java:5191)
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
01-22 17:10:50.954: E/AndroidRuntime(30757): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
01-22 17:10:50.954: E/AndroidRuntime(30757): ... 11 more
Alles anzeigen
Die Mainactivity wurde ja automatisch generiert. hab da nichts geändert.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
// -------------------------------------------------------------------------------
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
// -------------------------------------------------------------------------------
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
}
Alles anzeigen
Zeile 34 ist actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Ich weiß nicht, ob man das so einfach übernehmen kann ( hab keine ahnung wie ich vorgehen muss). Im video war es ja schließlich eine blank activity. Habe schon paar tutorials mir angesehen, aber die enthielten nicht das ergebnis, was ich mir wünsche bzw. waren einige unvollständig. hier sind die tutorials, die mich ziemlich verwirrt haben
http://blog.alwold.com/2013/08…n-the-android-action-bar/
http://www.androidhive.info/20…-working-with-action-bar/
https://developer.android.com/…cs/actionbar/styling.html