问题
I'm a Beginner Android Programmer and I'm toying around with JFeinstein's awesome sliding menu, trying to figure out how it works and implement it in my apps but in all of my implementations the behind view covers 100% of the above view.
Also, the only way to go back to the above view is to press the back button, so no "sliding"
back either. If I understand correctly, this should be controlled by the BehindViewOffset
, yet I don't seem to be getting it to work. Anyway, here's a little sample code:
Here's my MainActivity
:
public class MainActivity extends SlidingActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.testing);
populate();
SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
setSlidingActionBarEnabled(true);
menu.setMenu(R.layout.testing);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void populate() {
ListView lv = (ListView) findViewById(R.id.listView1);
String[] values = new String[] { "One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
lv.setAdapter(adapter);
}
}
And the layout 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" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Sadly I can't post a screenshot until I earn more rep!
Edit: Added the dimen.xml
<resources>
<dimen name="slidingmenu_offset">120dp</dimen>
<dimen name="list_padding">10dp</dimen>
<dimen name="shadow_width">15dp</dimen>
<integer name="num_cols">1</integer>
</resources>
回答1:
Ok, so I took a close look at the example and finnaly got it working by changing
SlidingMenu menu = new SlidingMenu(this);
for:
SlidingMenu menu = getSlidingMenu();
So here is the final code for the main activity´s onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.testing);
populate();
SlidingMenu menu = getSlidingMenu();
menu.setMode(SlidingMenu.LEFT);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setSlidingActionBarEnabled(true);
}
来源:https://stackoverflow.com/questions/14162683/slidingmenu-not-showing-properly