繁体   English   中英

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

[英]Problems with implementing custom actionbar android

我在下面创建了一个自定义操作栏布局。 我想在我的一项活动中实现这一目标(目的是在所有活动中都实现该目标,但现在我正在测试其中一项)。 我使用以下代码执行此操作:

  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); } 

但是,我没有得到动作栏,而是得到了看起来像这样的东西的混搭,甚至还没有填满整个屏幕。 在此处输入图片说明

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> 

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

您未填满屏幕宽度是什么意思? 混搭是因为您试图将许多嵌套的LinearLayouts和其他内容一起压缩到一个ActionBar中。 右边的三个点是溢出菜单(可以禁用)。

请查看Android设计指南,以正确使用工具栏(因为不推荐使用ActionBar)。 我认为您更多的是设计问题,而不是编程问题。

您试图在操作栏中添加很多视图。通常,操作栏布局简单,整洁。大多数情况下,操作栏的目的是显示重要的更新和导航视图。 如果您仍然需要此GUI,我认为您可以放弃此操作栏GUI,而创建可重用的自定义布局并将其放在每个活动的顶部。

暂无
暂无

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

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