在动作条机器人中心的自定义视图 [英] Android center custom view in actionbar

查看:131
本文介绍了在动作条机器人中心的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试设置的自定义视图为我的动作条,但具有的问题是它不会被填满,这使得它哈日我居中对象动作条的整个宽度。如何设置我的动作条自定义视图,填补了整个动作条?

$ C $下建立我的动作条:

  actionBar.setDisplayShowHomeEnabled(假);
actionBar.setDisplayShowCustomEnabled(真正的);
actionBar.setDisplayShowTitleEnabled(假);

LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionBar.setCustomView(inflater.inflate(R.layout.actionbar_layout,NULL));
 

和我actionbar_layout.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:后台=#EDEDED>

<的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID /标题
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真
    机器人:文字颜色=#FFFFFF
    机器人:文本=测试/>
< / RelativeLayout的>
 

解决方案

据我了解,事情是,上布局总是streched到内部视图的身高和体重。你可以把它仅仅通过设置layout_height =match_parent工作 在TextView的:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

    <的TextView
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:重力=中心
        机器人:文本=@字符串/ actionbar_title
        />
< / LinearLayout中>
 

Trying to set a custom view for my actionbar, but have the problem that it doesn't fill up the entire width of the actionbar which makes it har for me to center objects. How can i set a custom view in my actionbar that fills up the entire actionbar?

Code for setting up my actionbar:

actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionBar.setCustomView(inflater.inflate(R.layout.actionbar_layout, null));

And my actionbar_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="#EDEDED">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/title"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textColor="#FFFFFF"
    android:text="Testing"/>
</RelativeLayout>

解决方案

As far as I am concerned, the thing is that the upper layout is always streched to the inner view height and weight. You can make it work just by setting layout_height="match_parent" in the TextView:

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

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/actionbar_title"
        />
</LinearLayout>

这篇关于在动作条机器人中心的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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