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

2023-09-12 00:23:27 作者:倾一座城、淡一场梦

所以,我刚刚更新了我的codeBase的棒棒糖,和我在使用操作栏的问题。我使用AppCompat和ActionBarActivity,以及膨胀的自定义视图。似乎定制视图不再占据屏幕的整个宽度,留下一个薄带在左

路是用来查找

方式,它现在看起来

这是在code我使用来设置操作栏。任何人有什么想法?

 最后的动作条动作条= getSupportActionBar();
如果(动作条!= NULL){
    actionBar.setDisplayHomeAsUpEnabled(假);
    actionBar.setDisplayShowHomeEnabled(假);
    actionBar.setDisplayShowTitleEnabled(假);
    actionBar.setDisplayShowCustomEnabled(真正的);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setCustomView(R.layout.action_bar_content_search_custom_view);
    actionBar.setBackgroundDrawable(空);
    // actionBar.setStackedBackgroundDrawable(空);
    TextView的标题=(TextView中)actionBar.getCustomView()findViewById(R.id.action_bar_title)。
    title.setText(R.string.youtube);
    ImageView的背=(ImageView的)actionBar.getCustomView()findViewById(R.id.action_bar_back)。
    back.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            完();
        }
    });
}
 

修改

取出的自定义视图和不断变化的背景,现在占据了整个宽度。所以,问题是,我们如何才能使一个CustomView占用动作条的整个宽度?

解决方案

看起来这是造成最近 appcompat-V7 更新。 好像有显著改变,你应该如何处理行动吧。

我面临着同样的问题,看完后的ActionBar文档,尤其是下面引用我找到了一个解决方案。

  

与Android L(API级别21)开始,行动起来吧,可以再通过应用程序布局中的任何工具栏插件psented $ P $。该应用程序可能预示到工具栏应被视为活动的操作栏上的活动。使用此功能,应使用所提供的.NoActionBar主题之一,​​设置windowActionBar属性设置为false或其他活动不要求窗口功能。

我看到它的方式,在 AppCompat 主题是改变,一方面似乎打破了一些东西,但提供其他更大的灵活性。 我建议按照以下步骤:

使用 .NoActionBar 的风格在你的活动在上述报价描述 添加 android.support.v7.widget.Toolbar 您的活动布局 将应用程序:contentInsetStart =0dp属性。 这是你在你的问题描写保证金的主要问题

< android.support.v7.widget.Toolbar的xmlns:机器人=htt​​p://schemas.android.com / APK / RES /安卓     的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto     机器人:ID =@ + ID /动作条     机器人:layout_width =match_parent     机器人:layout_height =WRAP_CONTENT     应用程序:contentInsetEnd =0dp     应用程序:contentInsetStart =0dp> < /android.support.v7.widget.Toolbar> Android移动开发技术文章 手机开发

它通常建议您这样做,在一个单独的布局文件,并使用包括在活动的布局,所以你只需要,如果用在一个地方自定义工具栏多重活动

 <包括布局=@布局/ view_action_bar/>
 

使用 findViewById setSupportActionBar 在活动的onCreate 信号到工具栏应被视为活动的操作栏上的活动

工具栏动作条=(工具栏)findViewById(R.id.actionBar); setSupportActionBar(动作条);

一旦你做到这一点,在 onCreateOptionsMenu 添加的所有行动将被添加到工具栏,它会被当作活动操作栏。 根据需要进一步自定义工具栏(添加子视图等。)

So, I just updated my codebase to Lollipop, and I'm having issues with the Action Bar. I'm using AppCompat and ActionBarActivity, and inflating a custom view. It seems that the custom view no longer takes up the whole width of the screen, leaving a thin strip on the left

Way it used to look

Way it looks now

This is the code I'm using to set up the Action Bar. Anyone have any ideas?

final ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setCustomView(R.layout.action_bar_content_search_custom_view);
    actionBar.setBackgroundDrawable(null);
    // actionBar.setStackedBackgroundDrawable(null);
    TextView title = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_title);
    title.setText(R.string.youtube);
    ImageView back = (ImageView) actionBar.getCustomView().findViewById(R.id.action_bar_back);
    back.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

Edit

Taking out the custom view and changing the background now takes up the whole width. So the issue is, how can we make a CustomView take up the whole width of the ActionBar?

解决方案

Looks like this is caused by the latest changes to the ActionBar in the recent appcompat-v7 update. It seems like that there are significant changes to how you should handle action bars.

I faced the same issue and after reading the ActionBar documentation, and especially the following quote I found a solution.

Beginning with Android L (API level 21), the action bar may be represented by any Toolbar widget within the application layout. The application may signal to the Activity which Toolbar should be treated as the Activity's action bar. Activities that use this feature should use one of the supplied .NoActionBar themes, set the windowActionBar attribute to false or otherwise not request the window feature.

The way I see it, the AppCompat themes were changed and on one hand seemed to break a few things but provide much more flexibility on the other. I recommend following these steps:

Use .NoActionBar style in your activity as described in the above quote Add a android.support.v7.widget.Toolbar to your Activity layout Set the app:contentInsetStart="0dp" attribute. This is the main issue with the margin that you describe in your question

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/actionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp" >
</android.support.v7.widget.Toolbar>

It's usually recommended that you do that in a separate layout file and use include in your activity layout so you will only need to customize the Toolbar in one place if used in multiple activities

<include layout="@layout/view_action_bar" />

Use findViewById and setSupportActionBar in your Activity onCreate to signal to the Activity which Toolbar should be treated as the Activity's action bar

Toolbar actionBar = (Toolbar) findViewById(R.id.actionBar);
setSupportActionBar(actionBar);

Once you do that, all actions added in onCreateOptionsMenu will be added to the toolbar and it will be treated as the activity action bar. Further customize the Toolbar as desired (Add child views etc.)