我想在我的安卓应用程序中将切换按钮图标设置为右侧。 这是我的代码。 我该怎么做?顺便说一下,我将android:layout_gravity
的值设置为右,现在我也想将抽屉的图标设置为右边。我该怎么做?My MainActivity 的类代码:
package com.examplde.menuefinal;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private String[] mPlanetTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
String dokme_txt= "Esfandune";
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mPlanetTitles = getResources().getStringArray(R.array.dokmeha);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mPlanetTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int post, long arg3) {
String dokme_txt = getResources().getStringArray(R.array.dokmeha)[post];
Toast.makeText(getBaseContext(),"برروی "+dokme_txt+" کلیک شد", Toast.LENGTH_SHORT).show();
mDrawerLayout.closeDrawer(mDrawerList);
}
});
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_launcher, R.string.drawer_open,
R.string.drawer_close) {
@Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(dokme_txt);
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("تنظیمات");
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
// tanzim e drawer toggle be surate DrawerListener baraye mDrawerLayout
// emun
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
可以通过以下代码执行此操作,但最低级别 API 为 17。将其添加到清单应用程序和活动标记
android:supportsRtl="true"
然后在主活动中设置内容视图之后,添加以下内容。 以在右侧制作汉堡包图标。
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);