我是Android的新手,但我认为我正在尝试做非常基本的事情并且它无法正常工作。
所以这是我的活动:
.jar
这是我的public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public TextView lblHelp;
public ImageButton mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
lblHelp = (TextView) findViewById(R.id.lbl_main_help);
mBtn = (ImageButton) findViewById(R.id.btn);
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Clicked:", v.toString());
}
});
updateUI();
}
@Override
protected void onResume(){
super.onResume();
updateUI();
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
} else if (id == R.id.nav_profile) {
} else if (id == R.id.nav_notifications) {
} else if (id == R.id.nav_social) {
} else if (id == R.id.nav_mobs) {
} else if (id == R.id.nav_signout) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void updateUI(){
lblHelp.setVisibility(User.getInstance().getSelected() == null ? TextView.GONE : TextView.VISIBLE);
}
}
:
activity_main.xml
这是我的<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/drawer_layout"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main2_drawer" />
<include layout="@layout/content_main" />
</android.support.v4.widget.DrawerLayout>
:
content_main.xml
所以,我有2个问题
1:当我点击它时,mBtn的onClick监听器永远不会被调用,虽然我在logcat中看到了这样的东西:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="org.app.MainActivity"
tools:showIn="@layout/app_bar_main2"
android:background="@mipmap/home_bg">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your MOB has helped raise"
android:id="@+id/lbl_main_help"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#227890"
android:layout_marginTop="23dp"
android:visibility="visible" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:background="@mipmap/btn"
android:layout_below="@+id/raised_container"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
另一方面,lblHelp永远不会从视图中消失,即使我在控制台中看到可见性从0变为8。
我必须在这里找到一些非常简单的东西.........
由于
修改
好的,我找到了一些东西。在activity_main.xml上,如果我发表评论,一切正常:
12-12 01:51:10.110 17475-17475/D/InputEventReceiver: dispatchM{ac=1,id[0]=0,(614,1088),tool=1,flags=0x0,pointCnt=1,evT=423145299,downT=423145224,dev=3,src=0x1002}
12-12 01:51:10.110 17475-17475/V/View: onTouchEvent android.support.v7.widget.AppCompatImageButton{42270e50 VFED..C. ...P.... 178,597-901,1320 #7f0e009c app:id/btn}
12-12 01:51:10.110 17475-17475/I/PhoneWindow: cb.dispatchTouchEvent handled
12-12 01:51:10.110 17475-17475/D/InputEventReceiver: finishM{evT=423145299}
知道为什么会这样,我该如何解决?
答案 0 :(得分:0)
从Log.e改变(&#34; Clicked:&#34;,v.toString());到Log.d(&#34; Clicked:&#34;,v.toString());并检查
答案 1 :(得分:0)
问题是您的content_main
布局设置为app_bar_main2
:
tools:showIn="@layout/app_bar_main2"
所以这一行:
<include layout="@layout/content_main" />
进入app_bar_main2.xml
布局,而不是当前位于activity_main.xml
。