设置一个ImageView的打开侧边栏/菜单的Android [英] Setup an imageview to open sidebar/menu android
问题描述
而不是使用动作条,并设立一个图标,我无法在这里使用动作条。我真正想要做的是设置一个ImageView的,这是为了响应用户的点击。当它被点击时,侧边栏/菜单打开或关闭,就像单击物理按钮,Android手机的按钮。
有没有简单的方法来做到这一点? (在侧边栏的所有项目都已经完成,做工精细。我通过点击物理菜单按钮,我的手机上查吧)
Instead of using actionbar and setting up an icon, I could not use actionbar here. What I really want to do is to setup an imageview, which responds to user's click. When it is clicked, sidebar/menu will open or close, just like clicking on physical button at the button of android phone.
Is there any easy way to do so? (all the items in the sidebar are already done and work fine. I checked it by clicking on physical menu button on my phone)
推荐答案
毛毡值得把code这个例子了上的 GitHub上,由于当时天色很长:
Felt worthwhile to put the code for this example up on GitHub, as it was getting quite long:
刚刚成立的 OnClickListener
你的的ImageView
,以同样的方式将一个按钮
。
Just set an OnClickListener
to your ImageView
, in the same way you would a Button
.
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setOnClickListener(this);
//...
@Override
public void onClick(View v) {
if(!mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.openDrawer(GravityCompat.START);
} else {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
}
您可能需要添加的android:点击=真正的
到 XML
您的ImageView
和的android:背景=机器人:ATTR / selectableItemBackground
单击该项目时获得的反馈
You may want to add android:clickable = "true"
to the xml
for your ImageView
, and android:background="?android:attr/selectableItemBackground"
to gain feedback when the item is clicked.
此假设你已经设置了DrawerLayout有点像下面这样:
This assumes you've set up your DrawerLayout somewhat like the following:
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout = null;
private ActionBarDrawerToggle mActionBarDrawerToggle = null;
private RecyclerView mRecyclerView = null;
private NavMenuAdapter adapter = null;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 0, 0);
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
mRecyclerView = (RecyclerView) findViewById(R.id.navigation_menu_recycler);
adapter = new NavMenuAdapter();
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
imageView = (ImageView) findViewById(R.id.image_view);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.openDrawer(GravityCompat.START);
} else {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
编辑:
如果您想了解更多的控制,您可以定义各种状态为您的ImageView
与状态列表绘制,并用它在的android:背景
而不是上述建议。在你的创建一个新的
文件夹(即 XML
文件绘制 custom_selector.xml
)用下面的模板:
If you would like more control, you can define various states for your ImageView
with a state list drawable, and use it in android:background
instead of the above suggestion. Create a new xml
file in your drawable
folder (i.e. custom_selector.xml
) with the following template:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/image_focused"
android:state_focused="true" />
<item android:drawable="@drawable/image_normal" />
</selector>
和定义pressed相关的图片,突出重点/悬停和正常状态。与应用它:
And define relevant images for pressed, focused/hover and normal states. Apply it with:
android:background="@drawable/custom_selector"
这篇关于设置一个ImageView的打开侧边栏/菜单的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!