安卓:从动作条的自定义布局中删除左边距自定义、布局、动作

2023-09-12 00:31:32 作者:独抱浓愁

我使用的是自定义的动作条视图,你可以在下面的截图中看到,有一个在动作条的空白的灰色空间。我想删除它。

我做了什么:

  

RES /值-V11 / styles.xml

 <样式名称=AppBaseTheme父=@风格/ Theme.AppCompat.Light>
        <项目名称=机器人:actionBarStyle> @风格/ ActionBarStyle< /项目>
        <项目名称=actionBarStyle> @风格/ ActionBarStyle< /项目>
< /风格>
 

  

RES /价值/ my_custom_actionbar.xml

 <资源的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <样式名称=ActionBarStyle父=@风格/ Widget.AppCompat.Light.ActionBar.Solid>
        <项目名称=机器人:身高> 60dp< /项目>
    < /风格>
< /资源>
 

  

清单

 <使用-SDK
        安卓的minSdkVersion =10
        机器人:targetSdkVersion =19/>

<应用
            机器人:图标=@可绘制/ ic_launcher
            机器人:标签=@字符串/ AppName的
            机器人:主题=@风格/ AppBaseTheme>
    <! - 活动...等 - >
< /用途>
 
Android 从操作栏的自定义布局中删除左侧边距

  

MainActivity

 公共无效的onCreate(束捆){
    super.onCreate(包);

    动作条动作条= getSupportActionBar();

    actionbar.setDefaultDisplayHomeAsUpEnabled(假);
    actionbar.setDisplayHomeAsUpEnabled(假);
    actionbar.setDisplayShowCustomEnabled(真正的);
    actionbar.setDisplayShowHomeEnabled(假);
    actionbar.setDisplayShowTitleEnabled(假);
    actionbar.setDisplayUseLogoEnabled(假);
    actionbar.setHomeButtonEnabled(假);

    //添加自定义布局
    查看查看= LayoutInflater.from(本).inflate(R.layout.actionbar,空,假);
    actionbar.setCustomView(视图);
}
 

我已经找到最近的文章中,即指出存在的问题与最新版本。我也更新ADT和SDK到Android 5。

Android ActionBar的自定义视图不填充父

我不知道我该怎么做。

修改(部分解决方案):

不工作在Android< = 10 API

Android棒棒糖,AppCompat动作条自定义视图不会占用整个屏幕宽度

我有什么改变:

使用最新的SDK版本:

 <使用-SDK
        安卓的minSdkVersion =10
        机器人:targetSdkVersion =21/>
 

添加 toolbarStyle

 <样式名称=AppBaseTheme父=@风格/ Theme.AppCompat.Light>
        <项目名称=机器人:actionBarStyle> @风格/ ActionBarStyle< /项目>
        <项目名称=actionBarStyle> @风格/ ActionBarStyle< /项目>

        <项目名称=机器人:toolbarStyle> @风格/ ToolbarStyle< /项目>
        <项目名称=toolbarStyle> @风格/ ToolbarStyle< /项目>
< /风格>

<样式名称=ToolbarStyle父=@风格/ Widget.AppCompat.Toolbar>
    <项目名称=contentInsetStart> 0dp< /项目>
    <项目名称=机器人:contentInsetStart> 0dp< /项目>
< /风格>
 

解决方案

试试这个:

 动作条动作条= getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(假);
    actionBar.setDisplayShowCustomEnabled(真正的);
    actionBar.setDisplayShowTitleEnabled(假);
    查看customView = getLayoutInflater()膨胀(R.layout.main_action_bar,空)。
    actionBar.setCustomView(customView);
    工具栏父=(工具栏)customView.getParent();
    parent.setContentInsetsAbsolute(0,0);
 

我用这个code在我的项目,好运气;

I am using a custom actionbar view, and as you can see in the screenshot below, there is a blank gray space in the actionbar. I want to remove it.

What have I done:

res/values-v11/styles.xml

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
        <item name="actionBarStyle">@style/ActionBarStyle</item>
</style>

res/values/my_custom_actionbar.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:height">60dp</item>
    </style>
</resources>

Manifest

<uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19" />

<application
            android:icon="@drawable/ic_launcher"
            android:label="@string/AppName"
            android:theme="@style/AppBaseTheme" >
    <!-- activities... etc -->
</application>

MainActivity

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    ActionBar actionbar = getSupportActionBar();

    actionbar.setDefaultDisplayHomeAsUpEnabled(false);
    actionbar.setDisplayHomeAsUpEnabled(false);
    actionbar.setDisplayShowCustomEnabled(true);
    actionbar.setDisplayShowHomeEnabled(false);
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setDisplayUseLogoEnabled(false);
    actionbar.setHomeButtonEnabled(false);

    // Add the custom layout
    View view = LayoutInflater.from(this).inflate(R.layout.actionbar, null, false);
    actionbar.setCustomView(view);
}

I have found a recent post, that is pointing out that there is an issue with the latest release. I have also updated ADT and SDK to Android 5.

Android ActionBar's custom view not filling parent

I don't know what should I do.

Edit (partial solution):

Not working on Android <= API 10.

Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width

What have I changed:

Use the latest sdk version:

<uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="21" />

Add a toolbarStyle:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
        <item name="actionBarStyle">@style/ActionBarStyle</item>

        <item name="android:toolbarStyle">@style/ToolbarStyle</item>
        <item name="toolbarStyle">@style/ToolbarStyle</item>
</style>

<style name="ToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
    <item name="contentInsetStart">0dp</item>
    <item name="android:contentInsetStart">0dp</item>
</style>

解决方案

try this:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    View customView = getLayoutInflater().inflate(R.layout.main_action_bar, null);
    actionBar.setCustomView(customView);
    Toolbar parent =(Toolbar) customView.getParent();
    parent.setContentInsetsAbsolute(0,0);

I used this code in my project,good luck;