Android ActionBar自定义布局样式

1993年

我刚刚从IOS开始进行android开发,然后再次陷入了问题。经过1天的尝试,我决定问问堆栈溢出的人。

所以我的问题是:

我有一个应用程序,在操作栏中(默认为不带夏洛克),我想要1个徽标和2行文字。通过互联网,我为操作栏设置了setCustomView。而且自定义xml已加载,但我无法获得正确的布局。

所以首先我设置动作栏:

private void setupActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);

    LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null); // layout which contains your button.

    actionBar.setCustomView(customNav, lp1);
}

比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="match_parent"
    android:layout_gravity="fill_horizontal"
    android:orientation="horizontal"
    android:background="@color/Blue">

    <TextView
        android:id="@+id/text_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_menu"
        android:textColor="@color/White"
        android:paddingLeft="5dp"
        android:layout_gravity="left"/>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/Icon"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/text_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_help"
        android:layout_gravity="right"
        android:textColor="@color/White"
        android:paddingRight="5dp"/>

</LinearLayout>

但是结果是我不需要的东西:

图片问题

那我忘记/做错了/我应该怎么做?

法里耶·阿菲夫

尝试将根布局更改为相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal">

<TextView
    android:text="left text"
    android:layout_toLeftOf="@+id/icon"
    android:layout_alignParentLeft="true"
    android:id="@+id/text_left"
    android:gravity="left"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"/>

<ImageView
    android:layout_centerHorizontal="true"
    android:id="@+id/icon"
    android:layout_width="10dp"
    android:layout_height="wrap_content"
    android:src="@drawable/bg_sunrise"
    android:layout_gravity="center"/>

<TextView
    android:text="Right txt"
    android:layout_toRightOf="@+id/icon"
    android:id="@+id/text_right"
    android:layout_width="match_parent"
    android:gravity="right"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:paddingRight="5dp"/> </RelativeLayout>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android ActionBar自定义布局样式

来自分类Dev

Android布局自定义

来自分类Dev

具有自定义布局的ActionBar在Android 4.4.2(KitKat)上不会占据全屏宽度

来自分类Dev

Android:ActionBar自定义问题

来自分类Dev

具有自定义布局按钮样式的Android对话appcompat

来自分类Dev

列表视图的Android自定义布局

来自分类Dev

android:自定义视图布局包装

来自分类Dev

Cordova自定义Android布局插件

来自分类Dev

Android ListView自定义行布局

来自分类Dev

自定义ListView的布局(Android)

来自分类Dev

Android AlertDialog自定义布局失真

来自分类Dev

android中按钮的自定义布局

来自分类Dev

Android取消自定义布局的alertDialog

来自分类Dev

Android上的自定义按钮样式

来自分类Dev

Android上的自定义按钮样式

来自分类Dev

Android ActionBar自定义操作视图工具提示

来自分类Dev

Android Actionbar中的自定义大徽标

来自分类Dev

如何在Android中使用xml自定义ActionBar?

来自分类Dev

具有搜索视图的Android自定义ActionBar

来自分类Dev

Android Actionbar 不应用自定义图像背景

来自分类Dev

基于baseAdapter的FRAGMENT中的Android自定义布局

来自分类Dev

Android外接键盘自定义布局,多余的按键

来自分类Dev

Android在片段中使用AlertDialog和自定义布局

来自分类Dev

Android设置自定义首选项布局

来自分类Dev

自定义布局完全不显示-Android

来自分类Dev

如何使用基本布局并在android中自定义

来自分类Dev

在Android上使用自定义颜色的圆角布局

来自分类Dev

Android通知自定义布局XML imageview未显示

来自分类Dev

Android Alertdialog自定义布局按钮onClickListener崩溃