简体   繁体   English

实现自定义动作栏android的问题

[英]Problems with implementing custom actionbar android

I've created a custom action bar layout below. 我在下面创建了一个自定义操作栏布局。 I want to implement this for one of my activities (the aim is to have it for all the activities but I'm testing with one for now). 我想在我的一项活动中实现这一目标(目的是在所有活动中都实现该目标,但现在我正在测试其中一项)。 I use the following code to do this: 我使用以下代码执行此操作:

  public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_all_bets); getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false); LayoutInflater mInflater = LayoutInflater.from(this); View customView=getLayoutInflater().inflate(R.layout.action_bar, null); getSupportActionBar().setCustomView(customView); getSupportActionBar().setDisplayShowCustomEnabled(true); } 

However, instead of getting the actionbar, I get a mashup of things looking like this which doesn't even fill the full width of the screen. 但是,我没有得到动作栏,而是得到了看起来像这样的东西的混搭,甚至还没有填满整个屏幕。 在此处输入图片说明

action_bar.xml action_bar.xml

 <?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="120dp" android:weightSum="100" android:background="#27044A" android:orientation="vertical"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_weight="15" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_weight="30" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textAlignment="center" android:textColor="#d35400" android:textStyle="bold" android:text="User"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textColor="#d35400" android:layout_gravity="center_vertical" android:text="tester@tester.com"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_weight="25" android:layout_width="wrap_content" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textAlignment="center" android:textColor="#d35400" android:textStyle="bold" android:text="Coins"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textColor="#d35400" android:text="60000"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_weight="25" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textAlignment="center" android:textStyle="bold" android:textColor="#d35400" android:text="Winnings"/> <TextView android:layout_width="match_parent" android:textColor="#d35400" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="35000"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_weight="20" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="33dp" android:text="Logout" android:textColor="#ffd35400" android:textSize="13sp"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="85" android:orientation="horizontal" android:weightSum="100"> <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="20" android:background="@drawable/selector" android:orientation="vertical" android:weightSum="100"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/football50" android:layout_weight="93"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="7" android:textStyle="bold" android:textColor="#d35400" android:text= "Bet Now!" android:gravity="center"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="20" android:background="@drawable/selector" android:orientation="vertical" android:weightSum="100"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src = "@drawable/chip50" android:layout_weight="93"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text = "My Bets" android:textStyle="bold" android:gravity="center" android:textColor="#d35400" android:layout_weight="7"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="20" android:background="@drawable/selector" android:orientation="vertical" android:weightSum="100"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/clover50" android:layout_weight="93"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text = "Coin Frenzy" android:textStyle="bold" android:gravity="center" android:textColor="#d35400" android:layout_weight="7"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/selector" android:layout_weight="20" android:orientation="vertical" android:weightSum="100"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/trophy50" android:layout_weight="93"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text = "Rankings" android:textStyle="bold" android:gravity="center" android:textColor="#d35400" android:layout_weight="7"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="20" android:orientation="vertical" android:background="@drawable/selector" android:weightSum="100"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/moneybag50" android:layout_weight="93"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Shop" android:textStyle="bold" android:textColor="#d35400" android:gravity="center" android:layout_weight="7"/> </LinearLayout> </LinearLayout> </LinearLayout> 

在此处输入图片说明 Intended look 预期外观

What do you mean by it not filling the width of the screen? 您未填满屏幕宽度是什么意思? The mashup is because you're trying to squeeze so many nested LinearLayouts and things together into one ActionBar. 混搭是因为您试图将许多嵌套的LinearLayouts和其他内容一起压缩到一个ActionBar中。 The three dots on the right is the overflow menu (which you can disable). 右边的三个点是溢出菜单(可以禁用)。

Check out the Android design guidelines for proper uses of the Toolbar (since ActionBar is deprecated). 请查看Android设计指南,以正确使用工具栏(因为不推荐使用ActionBar)。 I think that you have more of a design issue than a programming issue. 我认为您更多的是设计问题,而不是编程问题。

You are trying to add lot of views in the action bar.Usually the action bar layout is simple and clean.Mostly the action bar purpose is to show important updates and navigational views. 您试图在操作栏中添加很多视图。通常,操作栏布局简单,整洁。大多数情况下,操作栏的目的是显示重要的更新和导航视图。 If you still need this GUI, I think you can discard this action bar GUI and instead create reusable custom layout and place it on top of every activity. 如果您仍然需要此GUI,我认为您可以放弃此操作栏GUI,而创建可重用的自定义布局并将其放在每个活动的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM