Подтвердить что ты не робот

Стиль пользовательского макета Android ActionBar

Я только начинаю разработку Android от IOS и снова сталкиваюсь с проблемой. И после 1 дня попытки я решил, что я попрошу людей.

Итак, моя проблема:

У меня есть приложение и в панели действий (по умолчанию нет sherlock) я хочу 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>

Но результат - это то, чего я не ищу:

Problem in an image

Итак, что я забываю/делаю неправильно/или должен ли я делать?

4b9b3361

Ответ 1

попробуйте изменить макет корня в относительный макет

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

Ответ 2

Вы не получаете свой результат, потому что:

enter image description here

вам нужно:

  1. если вы используете тему AppCompat

enter image description here

  1. добавить в свой макет деятельности

enter image description here

  1. в коде, например в onCreate() установить панель инструментов для замены панели действий.

enter image description here