右上角的菜单选项 [英] Right corner menu option

查看:227
本文介绍了右上角的菜单选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能设置背景图片的标题栏,我可以只设置颜色,让我知道我必须在下面的code改变。 我希望有一个确切如下图1复制,输出我收到见图2

         

 < ImageView的
        机器人:ID =@ + ID /头
        机器人:背景=@可绘制/ ic_launcher
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=展示厅
        机器人:文字颜色=@机器人:彩色/黑白
        机器人:TEXTSTYLE =黑体/>

    < ImageView的
        机器人:ID =@ + ID /头1
        机器人:drawableRight =@可绘制/菜单
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=按钮/>
< / LinearLayout中>
 

和我的活动中的java文件

 公共类ItemActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        的setContentView(R.layout.itemlist);

        。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.item);
    }
}
 

和styles.xml

 <样式名称=TitleBarTheme父=安卓主题>
            <项目名称=机器人:windowTitleSize> 35dip< /项目>
            <项目名称=机器人:windowTitleBackgroundStyle> @风格/ CustomWindowTitleBarBG< /项目>
        < /风格>
 

解决方案

对于文本对齐,这应该做到这一点。

 < TextView的机器人:ID =@ + ID / left_text
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:重力=中心
    机器人:layout_centerHorizo​​ntal =真
    机器人:文本=展示厅/>
 

至于动作条,首先添加一个XML自定义布局,这样的布局文件夹: custom_actionbar_layout

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向>

<的TextView
    机器人:ID =@ + ID / action_bar_title
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:文字颜色=#FFFFFF
    机器人:重力=中心
    机器人:文本=@字符串/ APP_NAME
    机器人:textAppearance =?机器人:textAppearanceLarge
    机器人:ellipsize =结束
    机器人:填充=5DP
    机器人:MAXLINES =1
    />
< / LinearLayout中>
 

而在你的活动:

 保护无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);
    的setContentView(R.Layout.xyz);
    initUI();
}
私人无效initUI(){
    动作条mActionBar = getSupportActionBar();

    mActionBar.setDisplayShowHomeEnabled(假);
    mActionBar.setIcon(R.drawable.ic_launcher);
    mActionBar.setDisplayShowCustomEnabled(真正的);
    mActionBar.setCustomView(R.layout.custom_actionbar_layout);
}
 

至于右侧动作条中的菜单图标,这就是所谓的菜单项,请尝试使用onCreateOptionsMenu,并设置showAsAction在它的XML到任何你想的充气功能,例如添加XML水库/像这样的菜单文件夹:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

<项目
    机器人:ID =@ + ID / ContctUs
    机器人:orderInCategory =100
    机器人:showAsAction =collapseActionView | withText
    机器人:标题=@字符串/联系我们
    机器人:图标=@可绘制/ ic_action_info/>

<项目
    机器人:ID =@ + ID / skin1
    机器人:orderInCategory =101
    机器人:showAsAction =collapseActionView | withText
    机器人:标题=@字符串/ skin1
    机器人:可见=假
    机器人:图标=@机器人:可绘制/ ic_menu_view/>
 

而在你的活动:

  @覆盖
公共布尔onCreateOptionsMenu(com.actionbarsherlock.view.Menu菜单){
    新MenuInflater(本).inflate(R.menu.main,菜单);
    返回super.onCreateOptionsMenu(菜单);
}
@覆盖
  公共布尔onOptionsItemSelected(菜单项项){
      如果(item.getItemId()== R.id.C​​ontctUs){
        startActivity(新意图(这一点,ContactUs.class));
      返回(真);
      }否则如果(...){...}
      返回super.onOptionsItemSelected(项目);
  }
 

希望这个作品。 注意: 这种方法用在preL设计,如果你想针对Android的-L,考虑使用工具栏。(support.v7.widget.Toolbar)

I am not able to set the background image for the title bar i can only set color ,let me know what i have to change in the below code. I want an exact below image 1 replica,the output i am getting is shown in figure 2

    <ImageView
        android:id="@+id/header"
        android:background="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ShowRoom"
        android:textColor="@android:color/black"
        android:textStyle="bold"/>

    <ImageView
        android:id="@+id/header1"
        android:drawableRight="@drawable/menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

and my activity java file

public class ItemActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.itemlist);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.item);
    }
}

and styles.xml

<style name="TitleBarTheme" parent="android:Theme">
            <item name="android:windowTitleSize">35dip</item>
            <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBarBG</item>
        </style>

解决方案

As for text Alignment,this should do it

<TextView android:id="@+id/left_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:text="ShowRoom" />

As for ActionBar, first Add an xml Custom layout to your layout folder like this: custom_actionbar_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/action_bar_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#ffffff"
    android:gravity="center"
    android:text="@string/app_name"
    android:textAppearance="?android:textAppearanceLarge"
    android:ellipsize="end"
    android:padding="5dp"
    android:maxLines="1"
    />
</LinearLayout>

And in your activity:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.Layout.xyz);
    initUI();
}
private void initUI(){
    ActionBar mActionBar = getSupportActionBar();

    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setIcon(R.drawable.ic_launcher);
    mActionBar.setDisplayShowCustomEnabled(true);
    mActionBar.setCustomView(R.layout.custom_actionbar_layout);
}

As for that Menu Icon on right side of ActionBar, it's called MenuItem, try inflating Menu using "onCreateOptionsMenu" and setting showAsAction in it's xml to whatever you wish for, for example add an xml to res/menu folder like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/ContctUs"
    android:orderInCategory="100"
    android:showAsAction="collapseActionView|withText"
    android:title="@string/ContactUs"
    android:icon="@drawable/ic_action_info"/>

<item
    android:id="@+id/skin1"
    android:orderInCategory="101"
    android:showAsAction="collapseActionView|withText"
    android:title="@string/skin1"
    android:visible="false"
    android:icon="@android:drawable/ic_menu_view"/>

And in your Activity:

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    new MenuInflater(this).inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
  public boolean onOptionsItemSelected(MenuItem item) {
      if (item.getItemId() == R.id.ContctUs) {
        startActivity(new Intent(this,ContactUs.class));
      return(true);
      }else if(...){...}
      return super.onOptionsItemSelected(item);
  }

Hope this works. Note: this method is used on Pre-L design, If you want to target android-L, consider using Toolbar.(support.v7.widget.Toolbar)

这篇关于右上角的菜单选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆