如何将RelativeLayout添加为菜单项 - Android

时间:2016-07-21 05:49:52

标签: android android-layout relativelayout android-xml android-menu

我有一个带有itemgroup菜单的XML。我打算在XML中添加一个RelativeLayout作为菜单项,以便它看起来像:

<menu>
    <item1/>
    <item2
         <RelativeLayout>
               <TextView1/>
               <TextView2/>
         <RelativeLayout>
    />
</menu>

这可以在Layout XML中完成,还是以编程方式完成?如果没有,解决方案会有所帮助。感谢。

3 个答案:

答案 0 :(得分:4)

使用您想要的视图创建布局文件,然后像这样使用它 -

<script>
    (function(t){function z(){for(var a=0;a<g.length;a++)g[a][0](g[a][1]);g=[];m=!1}function n(a,b){g.push([a,b]);m||(m=!0,A(z,0))}function B(a,b){function c(a){p(b,a)}function h(a){k(b,a)}try{a(c,h)}catch(d){h(d)}}function u(a){var b=a.owner,c=b.state_,b=b.data_,h=a[c];a=a.then;if("function"===typeof h){c=l;try{b=h(b)}catch(d){k(a,d)}}v(a,b)||(c===l&&p(a,b),c===q&&k(a,b))}function v(a,b){var c;try{if(a===b)throw new TypeError("A promises callback cannot return that same promise.");if(b&&("function"===
            typeof b||"object"===typeof b)){var h=b.then;if("function"===typeof h)return h.call(b,function(d){c||(c=!0,b!==d?p(a,d):w(a,d))},function(b){c||(c=!0,k(a,b))}),!0}}catch(d){return c||k(a,d),!0}return!1}function p(a,b){a!==b&&v(a,b)||w(a,b)}function w(a,b){a.state_===r&&(a.state_=x,a.data_=b,n(C,a))}function k(a,b){a.state_===r&&(a.state_=x,a.data_=b,n(D,a))}function y(a){var b=a.then_;a.then_=void 0;for(a=0;a<b.length;a++)u(b[a])}function C(a){a.state_=l;y(a)}function D(a){a.state_=q;y(a)}function e(a){if("function"!==
            typeof a)throw new TypeError("Promise constructor takes a function argument");if(!1===this instanceof e)throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");this.then_=[];B(a,this)}var f=t.Promise,s=f&&"resolve"in f&&"reject"in f&&"all"in f&&"race"in f&&function(){var a;new f(function(b){a=b});return"function"===typeof a}();"undefined"!==typeof exports&&exports?(exports.Promise=s?f:e,exports.Polyfill=e):"function"==
    typeof define&&define.amd?define(function(){return s?f:e}):s||(t.Promise=e);var r="pending",x="sealed",l="fulfilled",q="rejected",E=function(){},A="undefined"!==typeof setImmediate?setImmediate:setTimeout,g=[],m;e.prototype={constructor:e,state_:r,then_:null,data_:void 0,then:function(a,b){var c={owner:this,then:new this.constructor(E),fulfilled:a,rejected:b};this.state_===l||this.state_===q?n(u,c):this.then_.push(c);return c.then},"catch":function(a){return this.then(null,a)}};e.all=function(a){if("[object Array]"!==
            Object.prototype.toString.call(a))throw new TypeError("You must pass an array to Promise.all().");return new this(function(b,c){function h(a){e++;return function(c){d[a]=c;--e||b(d)}}for(var d=[],e=0,f=0,g;f<a.length;f++)(g=a[f])&&"function"===typeof g.then?g.then(h(f),c):d[f]=g;e||b(d)})};e.race=function(a){if("[object Array]"!==Object.prototype.toString.call(a))throw new TypeError("You must pass an array to Promise.race().");return new this(function(b,c){for(var e=0,d;e<a.length;e++)(d=a[e])&&"function"===
    typeof d.then?d.then(b,c):b(d)})};e.resolve=function(a){return a&&"object"===typeof a&&a.constructor===this?a:new this(function(b){b(a)})};e.reject=function(a){return new this(function(b,c){c(a)})}})("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this);
    </script>

编辑文字视图 -

<item
    android:id="@+id/menu_refresh"
    android:title="@string/refresh"
    yourapp:showAsAction="never"
    android:actionLayout="@layout/my_custom_layout"/>

答案 1 :(得分:3)

重点:

要使用Relativelayout或任何其他布局,您必须使用actionLayout作为@ Jack的延迟代码答案。但请记住。

android:actionLayout="@layout/my_custom_layout"不起作用,您必须使用app:actionLayout="@layout/my_custom_layout"

因为如果您使用ActionbarSherlockAppCompatandroid:命名空间将不适用于MenuItems。这是因为这些库使用模仿Android API的自定义属性,因为它们在早期版本的框架中不存在。

答案 2 :(得分:0)

你不能直接把它放在menu.xml中,你必须使用android:actionLayout属性。

它的工作原理如下:

menu.xml文件

SELECT DISTINCT
    DATE(A.found_time) as date,
    ISNULL(B.total, 0) AS total
FROM
    sent_bulletin_list A LEFT JOIN 
    (
        SELECT
            DATE(found_time) as date,
            COUNT( * ) as total
        FROM
            sent_bulletin_list
        WHERE
            found_time > ( UNIX_TIMESTAMP() - ( 7 * 24 * 60 * 60 ) )
        GROUP BY
            DATE(found_time);
    ) B ON DATE(A.found_time) = B.date

custom_layout.xml

<menu>
    <item ... android:actionLayout="@layout/custom_layout"/>
</menu>
相关问题