带有两个的FrameLayout仅包含正确设置的第一个。为什么?

时间:2018-04-25 18:27:04

标签: android layout

我有一个带有FrameLayout的ScollView布局,其中包含两种不同的布局。如果满足某些条件,我将其中一个设置为可见。 dialog_interface_login.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_white"
android:orientation="vertical">


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <include layout="@layout/landing_login_view"/>
    <include layout="@layout/mobile_login_view"/>
</FrameLayout>
</ScrollView>

我的课程:

final View v = inflater.inflate(R.layout.dialog_interface_login, container, false);
    View mobileLayout = v.findViewById(R.id.mobile_root_view);
    View landingLayout = v.findViewById(R.id.landing_root_view);
    if (landingLogin) {
        mobileLayout.setVisibility(View.INVISIBLE);
        landingLayout.setVisibility(View.VISIBLE);
    } else {
        mobileLayout.setVisibility(View.VISIBLE);
        landingLayout.setVisibility(View.INVISIBLE);
    }

在include上设置的theese布局中,我有另一个包括: landing_login_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:id="@+id/landing_root_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_white"
android:paddingLeft="@dimen/twenty_eight_dp"
android:paddingRight="@dimen/twenty_eight_dp"
android:orientation="vertical">

...

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

...
</LinearLayout>

和mobile_login_view.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:id="@+id/mobile_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_white"
android:paddingLeft="@dimen/twenty_eight_dp"
android:paddingRight="@dimen/twenty_eight_dp"
android:orientation="vertical">

...

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

</LinearLayout>

这是我的sms_login.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_white"
android:orientation="vertical">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/fifty_six_dp"
    android:layout_marginBottom="@dimen/twelve_dp"
    android:background="@drawable/btn_white_enabled">

    <br.com.fs.fslogin.ui.support.views.GothamEditText
        android:id="@+id/et_phone_number"
        style="@style/AppTheme.RectangularEditText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:digits="0123456789"
        android:gravity="center"
        android:hint="@string/hint_enter_phone"
        android:inputType="number|none"
        android:maxLength="11"
        android:textColorHint="@color/task_done_color"
        android:textSize="@dimen/sixteen_sp"
        font:name="@string/font_gotham_medium" />

</FrameLayout>

<FrameLayout
    android:id="@+id/dialog_error_phone"
    android:layout_width="match_parent"
    android:layout_height="@dimen/thirty_two_dp"
    android:layout_marginBottom="@dimen/twelve_dp"
    android:background="@drawable/bg_warning_phone"
    android:visibility="gone">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/eight_dp"
        android:layout_marginStart="@dimen/eight_dp"
        android:contentDescription="@null"
        android:src="@drawable/ic_error_phone_red" />

    <br.com.fs.fslogin.ui.support.views.GothamTextView
        android:id="@+id/error_phone_output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:lineSpacingExtra="@dimen/two_sp"
        android:textColor="@color/error_login_phone_color"
        android:textSize="@dimen/fourteen_sp"
        font:name="@string/font_gotham_medium" />

</FrameLayout>

<FrameLayout
    android:id="@+id/btn_sms_login"
    android:layout_width="match_parent"
    android:layout_height="@dimen/fifty_six_dp"
    android:background="@drawable/btn_login_vivo_purple">

    <br.com.fs.fslogin.ui.support.views.GothamButton
        style="@style/AppTheme.RectangularButton"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="@null"
        android:clickable="false"
        android:gravity="center"
        android:text="@string/btn_login"
        android:textColor="@android:color/white"
        android:textSize="@dimen/eighteen_sp"
        font:name="@string/font_gotham_bold" />
</FrameLayout>

</LinearLayout>

以编程方式,我设置了一些行为,例如手机字段的掩码等。 但是,只有在我的dialog_interface_login.xml中首先设置布局时,才会在sms_login.xml中以编程方式配置这些行为。我有相同的包含,具有相同的信息,但只适用于在ContainerLayout定义的第一个。即使是onclick事件也不会触发。

你知道为什么吗?是否可以定义两个不同的包含?

-----编辑----- 包括一些已完成的行动

v.findViewById(R.id.btn_sms_login).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (phoneWatcher.isPhoneNumberValid()) {
                String phoneNumber = etPhoneNumber.getText().toString().replaceAll("\\D+", "");
                onSuccessClick.OnSuccess(phoneNumber);
                LoginInterfaceVivoSyncDialog.this.dismiss();
            }
        }
    });

2 个答案:

答案 0 :(得分:0)

  

你知道为什么吗?

这是因为findViewById(R.id.xyz)会返回具有请求的属性View的第一个android:id="@+id/R.id.xyz"

从视图上的documentation引用:

  

查找具有给定ID的第一个后代视图,如果ID与getId()匹配则视图本身;如果ID无效(&lt; 0)或者层次结构中没有匹配的视图,则查找null。

要访问正确的View,您必须使用一些使其独一无二的属性。在您的情况下,例如,R.id.et_phone_number中可以找到值FrameLayout两次。但是如果你写的话

View myView = mobileLayout.findViewById(R.id.et_phone_number);

然后只有一个 mobileLayout 的子View具有请求的ID。

您的代码段可以更改如下:

final View v = inflater.inflate(R.layout.dialog_interface_login, container, false);
    View mobileLayout = v.findViewById(R.id.mobile_root_view);
    View landingLayout = v.findViewById(R.id.landing_root_view);
    if (landingLogin) {
        mobileLayout.setVisibility(View.INVISIBLE);
        landingLayout.setVisibility(View.VISIBLE);
    } else {
        mobileLayout.setVisibility(View.VISIBLE);
        landingLayout.setVisibility(View.INVISIBLE);
    }
    setupViewGroup(mobileLayout);
    setupViewGroup(landingLayout);

方法setupViewGroup(View layout)应如下所示:

private void setupViewGroup(View layout)
{
    final EditText etPhoneNumber = layout.findViewById(R.id.et_phone_number);
    layout.findViewById(R.id.btn_sms_login).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (phoneWatcher.isPhoneNumberValid()) {
                String phoneNumber = etPhoneNumber.getText().toString().replaceAll("\\D+", "");
                // ...
            }
        }
    });
}

通过在布局上调用findViewById(),您将获得使用View <添加的ViewGroup s的所需子<include .../> / p>

答案 1 :(得分:0)

  • 您应该使用Visibility.GONE代替INVISIBLE
  • 您可以直接指定ID以包含阻止为什么您制作两个额外的不同布局以仅提供ID。
  • 我很震惊地看到@dimen/twelve_dp非常震惊)。如果你在那里写dimens.xmltwelve_dp的用途是什么。您可以直接撰写12dp。它应该是通用的,如space_smallspace_largetext_size_small
  • 还从xml中删除了一些额外的包装布局。

<强> dialog_interface_login.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_white"
    android:orientation="vertical">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include
            android:id="@+id/landing_root_view"
            layout="@layout/sms_login" />
        <include
            android:id="@+id/mobile_root_view"
            layout="@layout/sms_login" />

    </FrameLayout>
</ScrollView>

final View v = inflater.inflate(R.layout.dialog_interface_login, container, false);
    View mobileLayout = v.findViewById(R.id.mobile_root_view);
    View landingLayout = v.findViewById(R.id.landing_root_view);
    if (landingLogin) {
        mobileLayout.setVisibility(View.GONE);
        landingLayout.setVisibility(View.VISIBLE);
    } else {
        mobileLayout.setVisibility(View.VISIBLE);
        landingLayout.setVisibility(View.GONE);
    }

<强> sms_login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:font="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_white"
    android:orientation="vertical">


    <br.com.fs.fslogin.ui.support.views.GothamEditText
        android:id="@+id/et_phone_number"
        style="@style/AppTheme.RectangularEditText"
        android:layout_width="match_parent"
        android:layout_height="@dimen/fifty_six_dp"
        android:layout_gravity="center"
        android:layout_marginBottom="@dimen/twelve_dp"
        android:background="@drawable/btn_white_enabled"
        android:digits="0123456789"
        android:gravity="center"
        android:hint="@string/hint_enter_phone"
        android:inputType="number|none"
        android:maxLength="11"
        android:textColorHint="@color/task_done_color"
        android:textSize="@dimen/sixteen_sp"
        font:name="@string/font_gotham_medium" />

    <FrameLayout
        android:id="@+id/dialog_error_phone"
        android:layout_width="match_parent"
        android:layout_height="@dimen/thirty_two_dp"
        android:layout_marginBottom="@dimen/twelve_dp"
        android:background="@drawable/bg_warning_phone"
        android:visibility="gone">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/eight_dp"
            android:layout_marginStart="@dimen/eight_dp"
            android:contentDescription="@null"
            android:src="@drawable/ic_error_phone_red" />

        <br.com.fs.fslogin.ui.support.views.GothamTextView
            android:id="@+id/error_phone_output"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:lineSpacingExtra="@dimen/two_sp"
            android:textColor="@color/error_login_phone_color"
            android:textSize="@dimen/fourteen_sp"
            font:name="@string/font_gotham_medium" />

    </FrameLayout>

    <br.com.fs.fslogin.ui.support.views.GothamButton
        android:id="@+id/btn_sms_login"
        style="@style/AppTheme.RectangularButton"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/fifty_six_dp"
        android:layout_gravity="center"
        android:background="@drawable/btn_login_vivo_purple"
        android:clickable="false"
        android:gravity="center"
        android:text="@string/btn_login"
        android:textColor="@android:color/white"
        android:textSize="@dimen/eighteen_sp"
        font:name="@string/font_gotham_bold" />

</LinearLayout>
相关问题