簡體   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