java - android 实现自定义操作栏的问题

标签 java android xml android-actionbar

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

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

但是,我没有得到操作栏,而是得到了看起来像这样的东西的混搭,它甚至没有填充屏幕的整个宽度。 enter image description here

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>

enter image description here 预期外观

最佳答案

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

查看 Android design guidelines为了正确使用工具栏(因为 ActionBar 已被弃用)。我认为你更多的是设计问题而不是编程问题。

关于java - android 实现自定义操作栏的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30715611/

相关文章:

xml - 如何将 xslt 中的参数用作 XPath?

java - 检查设备是否有互联网连接

java - 虽然呈现 ="false",但内容为 h :dataTable is always evaluated

java - 使用 Spring Boot JPA 高效合并两个表

Android 服务意外终止

android - Android Moto G 手机上的 Kivy QPython 应用程序 - 无日志输出

java - Android View InflateException 在尝试启动应用程序时出现

java - 人脸检测并使用边界框提取人脸并创建新的位图

Java 相当于 C# Linq 中的 Where 子句

android - BluetoothSocket.isConnected 问题