沒有你的代碼真的很難明白,爲什麼你不能ID找到,但我可以提供這樣說:
HomeButtonActivity.java
public class HomeButtonActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Button home_button = (Button) findViewById(R.id.home_button);
home_button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(HomeButtonActivity.this, "Replace with your own action", Toast.LENGTH_SHORT).show();
}
});
}
}
Activity1.java
public class Activity1 extends HomeButtonActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity1);
super.onCreate(savedInstanceState);
}
}
活性1 .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="com.example.myapplication.HomeButtonActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<include layout="@layout/home_button" />
</android.support.design.widget.CoordinatorLayout>
home_button.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="home button"/>
您應該有一個基類,它從一個活動擴展而來,並且爲所有活動做你想做的事情,然後可以從該活動擴展class(BaseActivity) –
對於android studio,我已經在擴展AppCompatActivity。我該怎麼辦? –
您可以創建一個名爲BaseActivity的抽象類並從AppCompatActivity擴展,然後您可以完成所有其他活動中的所有內容,然後可以從BaseActivity擴展其他活動,那就是。 –