具有自定义布局的Android ActionBar不占用父级的整个宽度

时间:2015-07-31 05:17:29

标签: java android xml

这是我的xml fil

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_gravity="fill_horizontal"
    android:gravity="fill_horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="fill_horizontal"
    android:background="#fff556ff"
    android:id="@+id/actionbar_relativelayout"
    >

    <ImageView
        android:clickable="true"
        android:id="@+id/imgMenu"
        android:layout_width="40dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp"
        android:paddingLeft="10dp"
        android:src="@drawable/menu_dashboard" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="22dp"
        android:layout_toEndOf="@+id/imgMenu"
        android:layout_toRightOf="@+id/imgMenu"
        android:text="@string/app_name"
        android:textColor="#ffffff"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/imgUser"
        android:layout_width="40dp"

        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_marginTop="15dp"

        android:src="@drawable/user" />

    <ImageView
        android:id="@+id/imgMessage"
        android:layout_width="40dp"
        android:clickable="true"
        android:layout_height="40dp"
        android:layout_marginTop="15dp"
        android:layout_toLeftOf="@+id/imgUser"
        android:src="@drawable/message_dashboard"
     />

    <ImageView
        android:id="@+id/imgInfo"
        android:layout_width="40dp"

        android:layout_height="40dp"
        android:layout_alignParentRight="true"

        android:layout_marginRight="80dp"
        android:layout_marginTop="15dp"
        android:src="@drawable/ic_action_search"
        android:clickable="true"/>
</RelativeLayout>
    </LinearLayout>

这是我的活动

 RelativeLayout custom_RelativeLayout = (RelativeLayout)findViewById(R.id.actionbar_relativelayout);
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM );
            getSupportActionBar().setCustomView(R.layout.custom_actionbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayShowCustomEnabled(true);
            getSupportActionBar().setDisplayUseLogoEnabled(false);
            getSupportActionBar().setDisplayShowHomeEnabled(false);
            getSupportActionBar().setBackgroundDrawable(
                    getApplicationContext().getResources().getDrawable(
                            R.drawable.drawable_actionbar_back));
            getSupportActionBar().setCustomView(R.layout.custom_actionbar);

我希望它占据整个动作栏的空间,但它不占用我也在stackoverflow上阅读了很多答案但是没有用。 1)我希望在操作栏中匹配父级空间

编辑这是我的主要Avtivity

package com.example.qasim.cashmanagement;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class DashboardActivity extends ActionBarActivity implements View.OnClickListener {

    View viewAction;
    ImageView imgUserbutton;// Actionbar
    ImageView imgMessagebutton;// Actionbar
    ImageView imgInfobutton;// Action bar
    ImageView imgMenubutton;// Actionbar

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_dashboard_two);
        android.support.v7.app.ActionBar actionbar = getSupportActionBar();
       GetActionBarDetails();// Method for inflating ActionBar

/*
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);*/

      /*  ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
        View actionBarView = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
        actionBar.setCustomView(actionBarView, lp);*/
     //  actionBar.setCustomView(R.layout.custom_actionbar);

    /*   getSupportActionBar().setCustomView(R.layout.custom_actionbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setBackgroundDrawable(
                getApplicationContext().getResources().getDrawable(
                        R.drawable.drawable_actionbar_back));

        viewAction = getSupportActionBar().getCustomView();*/
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    private void GetActionBarDetails()
    {

       RelativeLayout custom_RelativeLayout = (RelativeLayout)findViewById(R.id.actionbar_relativelayout);
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM );
        getSupportActionBar().setCustomView(R.layout.custom_actionbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setBackgroundDrawable(
                getApplicationContext().getResources().getDrawable(
                        R.drawable.drawable_actionbar_back));
        getSupportActionBar().setCustomView(R.layout.custom_actionbar);
        imgUserbutton = (ImageView) findViewById(R.id.imgUser);
        imgInfobutton = (ImageView) findViewById(R.id.imgInfo);
        imgMessagebutton = (ImageView)findViewById(R.id.imgMessage);
        imgMenubutton = (ImageView)findViewById(R.id.imgMenu);
        imgMenubutton.setOnClickListener(this);
        imgMessagebutton.setOnClickListener(this);
        imgInfobutton.setOnClickListener(this);
        imgUserbutton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

    }
}

1 个答案:

答案 0 :(得分:0)

如果您想获得原生操作栏的高度,请在XML中使用<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="#fff556ff" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="1" > <ImageView android:id="@+id/imgMenu" android:layout_width="0dp" android:layout_height="30dp" android:layout_gravity="center_vertical" android:layout_weight="0.15" android:clickable="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="0.4" android:text="@string/app_name" android:textColor="#ffffff" android:textSize="20sp" /> <ImageView android:id="@+id/imgUser" android:layout_width="0dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_weight="0.15" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/imgMessage" android:layout_width="0dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_weight="0.15" android:clickable="true" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/imgInfo" android:layout_width="0dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_weight="0.15" android:clickable="true" android:src="@drawable/ic_launcher" /> </LinearLayout> </RelativeLayout>

现在您的XML将如下所示:

{{1}}