我使用了android教程中的确切示例:http://developer.android.com/training/implementing-navigation/nav-drawer.html
构建导航抽屉。在这个活动中,我想要一个可点击的按钮,我有代码。但是当我点击按钮时,没有任何反应。我不明白为什么这不起作用。
我试图在没有运气的情况下在活动之上夸大另一个视图,我不太确定还有什么可以尝试让我的按钮可点击!
我的导航抽屉效果很好,只是按钮不可点击有问题。因此,给出的代码是最小的:
极简主义活动(HomeActivity.java)
public class HomeActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* The order must be maintained:
* 1) setContext
* 2) initActivityManager
* 3-N) anything else
*/
Singletons.ContextManager.setContext(getApplicationContext());
Singletons.AppManager.initActivityManager();
// This has to happen before the services start
Singletons.Coordinator.setScreenState(true);
// Inflate our layout
setContentView(R.layout.activity_app_drawer);
// Its always useful to keep this saved somewhere
this.appContext = this;
// Initialize our drawer
this.title = "Home";
// These two must always align
this.fragmentTitles = new String[]{"Home", "View App", "View History", "View Location", "Manage Services"};
this.fragmentClasses = new String[]{
"edu.ucr.aum.fragments.HomeFragment",
"edu.ucr.aum.fragments.AppStatsFragment",
"edu.ucr.aum.fragments.BrowserStatsFragment",
"edu.ucr.aum.fragments.LocationFragment",
"edu.ucr.aum.fragments.ServicesFragment"};
this.drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
this.drawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for our drawer
this.drawerList.setAdapter(
new ArrayAdapter<String>(
this,
R.layout.ad_text_view,
this.fragmentTitles
)
);
// Set the click listener
this.drawerList.setOnItemClickListener(new DrawerItemClickListener());
this.drawerToggle = new ActionBarDrawerToggle(
this,
this.drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(title);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(title);
}
};
this.drawerLayout.setDrawerListener(this.drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
/** Section for my button **/
Button exitButton = (Button) findViewById(R.id.btnStartExitSurvey);
long installDate = this.preferences.getLong("INSTALL_TIME", System.currentTimeMillis());
TextView tv = (TextView) findViewById(R.id.tvExitSurveyInstructions);
tv.setText(Html.fromHtml(getString(R.string.homeInstructions)));
if(Singletons.Coordinator.displayExitSurvey(installDate)) {
exitButton.setVisibility(View.VISIBLE);
exitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(appContext, SurveyActivity.class);
startActivity(i);
}
});
} else {
exitButton.setVisibility(View.INVISIBLE);
}
}
}
我的xml文件(activity_app_drawer.xml):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="fill_parent"
android:layout_width="wrap_content">
<TextView
android:id="@+id/tvExitSurveyInstructions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/homeInstructions"
/>
<Button
android:id="@+id/btnStartExitSurvey"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/homeButtonStartExitSurvey"
android:layout_below="@+id/tvExitSurveyInstructions"
/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
答案 0 :(得分:3)
您的布局似乎有误。 DrawerLayout
应该是父级和内容2或3个孩子:
FrameLayout
。所以,你的布局应该是这样的:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<!-- The main content view -->
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<TextView
android:id="@+id/tvExitSurveyInstructions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/homeInstructions"
/>
<Button
android:id="@+id/btnStartExitSurvey"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/homeButtonStartExitSurvey"
android:layout_below="@+id/tvExitSurveyInstructions"
/>
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"
/>
</android.support.v4.widget.DrawerLayout>