gpt4 book ai didi

java - 工具栏 addView 不起作用

转载 作者:行者123 更新时间:2023-12-02 12:06:52 26 4
gpt4 key购买 nike

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

我的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;
}

} }

我的 MainActivityXML:

<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:

<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>

我的应用程序主题:

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>

最佳答案

最简单的方法是采用整个布局 item_toolbar_main并将其设置为 Toolbar直接在 xml 中而不使用任何 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_mainandroid.support.v7.widget.Toolbar 的内部而不是<include layout="@layout/item_toolbar_main"> .

关于java - 工具栏 addView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46844355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com