【问题标题】:Manually inflating custom view yields different layouts for ActionBar custom view手动膨胀自定义视图会为 ActionBar 自定义视图产生不同的布局
【发布时间】:2013-07-08 08:07:38
【问题描述】:

来自资源的自定义视图:

// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setCustomView(R.layout.custom_action_bar);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

结果是:

自定义视图手动充气:

// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_action_bar, null);
actionBar.setCustomView(view);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

结果是:

custom_action_bar.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:orientation="horizontal" 
    android:weightSum="3">

    <TextView 
        android:id="@+id/bar_title1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/White"
        android:text="title1"/>
    <TextView 
        android:id="@+id/bar_title2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/White"
        android:text="title2"/>
    <TextView 
        android:id="@+id/bar_title3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/White"
        android:text="title3"/>

</LinearLayout>

这里显示的第一个布局是正确的,因为它的小部件上有权重。第二次尝试应该会产生相同的结果,但事实并非如此。

【问题讨论】:

  • 你能在 custom_action_bar.xml 中更改吗?我已经更改了 android:layout_width = fill_parent 并删除了 android:weightSum=3 schemas.android.com/apk/res/android" android:layout_width="fill_parent" android: layout_height="match_parent" android:orientation="horizo​​ntal" > 如果对你有用,你能分享一下结果吗?
  • @Rollno1 我试过了。您的解决方案有效,不需要fill_parent 部分(fill_parent 相当于match_parent)。删除 weightsum 属性就可以了。

标签: android android-actionbar android-custom-view android-inflate


【解决方案1】:

其实这里的问题是,在第二种情况下,ActionBar 需要额外的布局参数:

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_action_bar, null);
    actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

所以,它涵盖了所有的 ActionBar 区域。看起来默认情况下WRAP_CONTENT llayout 参数被应用于自定义视图。

【讨论】:

  • 有趣。我试过你的代码,你是对的。即使自定义布局已经为宽度和高度定义了match_parent
  • 对,但是为了应用父 ViewGroup 应该传递给 inflate()。实际上,ActionBarImpl 在方法 setCustomView(int resId) 中使用内部 ViewGroup - 例如setCustomView(LayoutInflater.from(getThemedContext()).inflate(resId, mActionView, false));
  • 将所有空间与我的自定义操作栏相匹配,我简直快疯了。最后,我按照 Sandrstar 的说法添加了 LayoutParams。
【解决方案2】:

另一种选择是让 ActionBar 为您完成工作(在这种情况下,我将展示如何使用支持版本来完成)。

// Set your custom view
getSupportActionBar().setCustomView(R.layout.custom_action_bar);

// Get the inflated view
View view = getSupportActionBar().getCustomView();

// Do what you want with the view (set the title, custom font etc.)
TextView actionBarTitle = (TextView) view.findViewById(R.id.action_bar_title);
...

// set the custom flag
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)

【讨论】:

  • 聪明的举动先生!恕我直言,这应该是公认的答案。使用带有“null”作为根视图的 Layout Inflater 是许多布局问题的“万恶之源”,因为与父相关的 layout_* 参数在膨胀时会被忽略。
  • 很好的答案!立即解决了我的布局问题。
  • @MaciejPigulski 'null' in root 不是根本原因,根本原因在于错过了为什么首先需要“root”,这是解决它的唯一方法 - RTFM。跨度>
  • @sandrstar,感谢您的宝贵见解,尤其是 RTFM 部分,非常有帮助!
猜你喜欢
  • 2013-07-24
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多