工具栏addView无法正常工作

时间:2017-10-20 07:34:30

标签: java android

我需要用自己的自定义Toolbar替换普通的MainActivity。 我使用以下代码但没有显示任何内容:

我的 public class MainActivity extends AppCompatActivity { private Toolbar mToolbar; private View mView; private TabLayout mTabLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); mView = LayoutInflater.from(this).inflate(R.layout.item_toolbar_main, null); mToolbar.addView(mView); ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); mTabLayout = (TabLayout) findViewById(R.id.tabs); mTabLayout.setupWithViewPager(viewPager); View view1 = View.inflate(this, R.layout.item_tablayout_item, null); view1.findViewById(R.id.icon).setBackgroundResource(R.drawable.ic_solved); View view2 = View.inflate(this, R.layout.item_tablayout_item, null); view2.findViewById(R.id.icon).setBackgroundResource(R.drawable.ic_unsolved) ; try { mTabLayout.getTabAt(0).setCustomView(mView); mTabLayout.getTabAt(1).setCustomView(mView); } catch (Exception ex) { //Add crashlitics; } } }

XML

我的MainActivity <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.divofmod.quizerfield.Activities.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:paddingBottom="@dimen/activity_margin_quarter" android:paddingLeft="@dimen/activity_margin_half" android:paddingRight="@dimen/activity_margin_half" app:contentInsetEnd="@dimen/activity_margin_zero" app:contentInsetLeft="@dimen/activity_margin_zero" app:contentInsetRight="@dimen/activity_margin_zero" app:contentInsetStart="@dimen/activity_margin_zero" app:layout_scrollFlags="scroll|enterAlways"/> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="fill" app:tabMode="fixed"/> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout>

XML

我的Toolbar <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true" > <ImageView android:id="@+id/logoQuizer" android:layout_width="match_parent" android:layout_height="@dimen/image_medium" android:layout_marginBottom="@dimen/activity_margin_half" android:layout_marginTop="@dimen/activity_margin_half" android:foregroundGravity="center_vertical" android:src="@drawable/logo_quizer"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/logoQuizer" android:orientation="horizontal"> <ImageView android:id="@+id/send" android:layout_width="@dimen/icon_small" android:layout_height="@dimen/icon_small" android:layout_alignParentLeft="true" android:layout_centerHorizontal="true" android:layout_marginLeft="@dimen/activity_margin_half" android:src="@drawable/ic_send"/> <ImageView android:id="@+id/sort" android:layout_width="@dimen/icon_small" android:layout_height="@dimen/icon_small" android:layout_centerHorizontal="true" android:layout_marginLeft="@dimen/activity_margin_quarter" android:layout_marginRight="@dimen/activity_margin_half" android:layout_toLeftOf="@+id/refresh" android:src="@drawable/ic_sort"/> <ImageView android:id="@+id/refresh" android:layout_width="@dimen/icon_small" android:layout_height="@dimen/icon_small" android:layout_alignParentRight="true" android:layout_centerHorizontal="true" android:layout_marginLeft="@dimen/activity_margin_quarter" android:layout_marginRight="@dimen/activity_margin_half" android:src="@drawable/ic_refresh"/> </RelativeLayout> </RelativeLayout> 项:

AppTheme

我的style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>

self.enc_batch = tf.placeholder(tf.int32, [hps.batch_size, None], name='enc_batch')

2 个答案:

答案 0 :(得分:1)

最简单的方法是获取整个布局item_toolbar_main并直接在xml中将其设置为Toolbar,而不使用任何Java代码。因此,将布局Toolbar中的activity_main.xml视图更改为以下内容:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
        android:paddingBottom="@dimen/activity_margin_quarter"
        android:paddingLeft="@dimen/activity_margin_half"
        android:paddingRight="@dimen/activity_margin_half"
        app:contentInsetEnd="@dimen/activity_margin_zero"
        app:contentInsetLeft="@dimen/activity_margin_zero"
        app:contentInsetRight="@dimen/activity_margin_zero"
        app:contentInsetStart="@dimen/activity_margin_zero"
        app:layout_scrollFlags="scroll|enterAlways">

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

</android.support.v7.widget.Toolbar>

这会奏效。我使用了<include>,但您也可以将整个View从布局item_toolbar_main复制并粘贴到android.support.v7.widget.Toolbar的内部,而不是<include layout="@layout/item_toolbar_main">。< / p>

答案 1 :(得分:0)

如果你想替换...为什么你不要只调用setSupportActionBar(theNewToolbar)来替换它?