gpt4 book ai didi

android - Android中常见的导航菜单抽屉

转载 作者:搜寻专家 更新时间:2023-11-01 08:40:46 24 4
gpt4 key购买 nike

我需要在我的 android 应用程序中添加抽屉导航菜单。我的应用程序有很多模块,所以我无法对每个 Activity 进行编码以显示导航菜单。所以我决定将导航菜单代码放在我的基本 Activity 中。所以每个 Activity 都扩展了这个基本 Activity 。抽屉导航菜单工作正常,但问题是 Activity 组件不工作。我不知道发生了什么。有什么需要改变的吗?提前致谢。

baseActivity.java 
public class BaseActivity extends ActionBarActivity {
RelativeLayout fullLayout;
DrawerLayout dLayout;

@Override
public void setContentView(int layoutResID) {
fullLayout = (RelativeLayout) getLayoutInflater().inflate(
R.layout.activity_base, null);
FrameLayout frameLayout = (FrameLayout) fullLayout
.findViewById(R.id.content_frame);
ListView dList = (ListView) fullLayout.findViewById(R.id.left_drawer);
dLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawer_layout);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
setContentView(fullLayout);

String[] menu = new String[] { "Home", "Android", "Windows", "Linux",
"Raspberry Pi", "WordPress", "Videos", "Contact Us" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, menu);

dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);

dList.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
dLayout.closeDrawers();
}
});
}};

activity_base.xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="sas.mobi.lakshmi.main.BaseActivity" >

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

homeActivity.java

public class HomeAndSettingActivity extends BaseActivity {
private Button btnAccount;
private Button btnCollection;
private Button btnOthers;
private Button btnTempExit;
private Button btnExit;
private AdminDS adminDS;
private AdminDO adminDO;
private FinanceDS financeDS;
private PartnerPaymentDS paymentDS;
private GoogleCloudMessaging gcm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_and_setting);
initializeComponents();
btnAccount.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
AccountRegHomeActivity.class);
startActivity(intent);
finish();
}
});
btnCollection.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
CollectionHomeActivity.class);
startActivity(intent);
finish();
}
});
btnOthers.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
OthersHomeActivity.class);
startActivity(intent);
finish();
}
});
btnTempExit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
finish();
}
});
btnExit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
financeDS = new FinanceDS(getApplicationContext());
boolean isUps = financeDS.closeActiveCurrentFinances();
if (isUps) {
finish();
}
}
});

}

/**
* method to initialize components
*/
private void initializeComponents() {
btnAccount = (Button) findViewById(R.id.btnAccount);
btnCollection = (Button) findViewById(R.id.btnCollection);
btnOthers = (Button) findViewById(R.id.btnOthers);
btnTempExit = (Button) findViewById(R.id.btnTempExit);
btnExit = (Button) findViewById(R.id.btnExit);
}};

acitivity_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<Button
android:id="@+id/btnAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="@string/btnAccount"
android:textSize="14sp" />

<Button
android:id="@+id/btnCollection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="@string/btnCollection"
android:textSize="14sp" />

<Button
android:id="@+id/btnOthers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="@string/btnOthers"
android:textSize="14sp" />

<Button
android:id="@+id/btnTempExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="@string/btnTempExit"
android:textSize="14sp" />

<Button
android:id="@+id/btnExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="@string/btnExit"
android:textSize="14sp" />

此主页 Activity 和公共(public)抽屉导航菜单显示正常,但主页 Activity 中的组件不工作。但抽屉组件正在工作。

最佳答案

将主xml改成这样,将sidemenu加载到side menu frame中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="sas.mobi.lakshmi.main.BaseActivity" >

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

<FrameLayout
android:id="@+id/side_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.v4.widget.DrawerLayout>

关于android - Android中常见的导航菜单抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32860003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com