如何在addView

时间:2018-07-07 00:10:01

标签: android android-layout android-view

我遇到了这个问题,我需要以编程方式将容器(ConstraintLayout)添加到工具栏,并强制父布局工具栏重新测量添加的子项的宽度,该宽度设置为match_parent。

action_bar_home.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/action_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/action_bar_height"
    android:background="@android:color/holo_blue_bright"
    android:paddingHorizontal="@dimen/padding_h_action_bar">

    <!-- Open Drawer-->
    <Button
        android:id="@+id/btn_open_drawer"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/nav_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- Title -->
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center_vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Apple Mobile" />

    <!-- Search -->
    <Button
        android:id="@+id/btn_search"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/search_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_settings"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- Settings -->
    <Button
        android:id="@+id/btn_settings"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/settings_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

addView

 View container = getLayoutInflater().inflate(R.layout.action_bar_home, null);

        Button btnOpenDrawer = container.findViewById(R.id.btn_open_drawer);
        btnOpenDrawer.setOnClickListener(v -> mMainActivity.openDrawer());

        if (mMainActivity.getToolbar().getChildCount() > 0) {
            mMainActivity.getToolbar().removeAllViews();
        }
        mMainActivity.getToolbar().addView(container);

通过这种方式,子容器(action_bar_home)不适合父容器的宽度。 如何解决这个问题? 谢谢大家。

0 个答案:

没有答案