【问题标题】:Custom Relative Layout in XML IssueXML问题中的自定义相对布局
【发布时间】:2015-08-12 15:00:59
【问题描述】:

我遇到了一个问题,我创建的自定义相对布局类导致父 XML 布局中的其他组件消失或不显示。

有什么我可以添加到布局中的东西,或者可以阻止列出的第一个 XML 代码中的所有其他布局/组件不显示的类吗?

XML 实现 - 工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.l.mapprototype.view.ToolbarLayout
        android:id="@+id/primary_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <View
        android:id="@+id/profile_sep_view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/primary_toolbar_layout"
        android:background="#ffcccccc" />

    <com.l.mapprototype.font.RobotoTextView
        android:id="@+id/ap_profile_not_found_tv"
        style="@style/TextViewAppearance.Body1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ap_action_btn_wrap"
        android:layout_below="@+id/profile_sep_view"
        android:gravity="center"
        android:text="Profile Not Found"
        android:visibility="gone"
        app:typeface="robotoBold" />

    <ScrollView
        android:id="@+id/ap_fields_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ap_action_btn_wrap"
        android:layout_below="@+id/profile_sep_view"
        android:visibility="visible" >

        <LinearLayout
            android:id="@+id/ap_fields_linearlayout_parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/ap_fields_linearlayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/bg_card"
                android:orientation="vertical"
                android:padding="10dp" >

                <LinearLayout
                    android:id="@+id/ap_first_name_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/ap_firstname_imageview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/user_icon" />

                    <EditText
                        android:id="@+id/ap_first_name_et"
                        style="@style/EditTextStyle.Standard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:ems="10"
                        android:hint="First Name"
                        android:inputType="textPersonName"
                        android:padding="10dp"
                        tools:ignore="HardcodedText" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ap_last_name_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/ap_lastname_imageview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/user_icon" />

                    <EditText
                        android:id="@+id/ap_last_name_et"
                        style="@style/EditTextStyle.Standard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:ems="10"
                        android:hint="Last Name"
                        android:inputType="textPersonName"
                        android:padding="10dp"
                        tools:ignore="HardcodedText" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ap_phonenum_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/ap_phonenum_imageview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/phonenumber_icon" />

                    <EditText
                        android:id="@+id/ap_phonenum_et"
                        style="@style/EditTextStyle.Standard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:ems="10"
                        android:hint="Phone Number"
                        android:inputType="phone"
                        android:padding="10dp"
                        tools:ignore="HardcodedText" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ap_email_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/ap_email_imageview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/email_icon" />

                    <EditText
                        android:id="@+id/ap_email_et"
                        style="@style/EditTextStyle.Standard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:ems="10"
                        android:hint="Email Address"
                        android:inputType="textWebEmailAddress"
                        android:padding="10dp"
                        tools:ignore="HardcodedText" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ap_username_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/ap_username_imageview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/user_icon" />

                    <EditText
                        android:id="@+id/ap_username_et"
                        style="@style/EditTextStyle.Standard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:ems="10"
                        android:hint="Username"
                        android:inputType="textPersonName"
                        android:padding="10dp"
                        tools:ignore="HardcodedText" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ap_password_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/ap_password_imageview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/password_icon" />

                    <EditText
                        android:id="@+id/ap_password_et"
                        style="@style/EditTextStyle.Standard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:ems="10"
                        android:hint="Password"
                        android:inputType="textPassword"
                        android:padding="10dp"
                        tools:ignore="HardcodedText" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ap_confirm_password_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/ap_confirm_password_imageview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/password_icon" />

                    <EditText
                        android:id="@+id/ap_confirm_passwork_et"
                        style="@style/EditTextStyle.Standard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:ems="10"
                        android:hint="Confirm password"
                        android:inputType="textPassword"
                        android:padding="10dp"
                        tools:ignore="HardcodedText" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ap_gratuity_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/bg_card"
                android:orientation="vertical"
                android:padding="10dp" >

                <LinearLayout
                    android:id="@+id/ap_gratuity_label_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <com.l.mapprototype.font.RobotoTextView
                        android:id="@+id/ap_gratuity_label"
                        style="@style/TextViewAppearance.Body2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:text="Default Gratuity Percent"
                        app:typeface="robotoBold" />

                    <com.l.mapprototype.font.RobotoTextView
                        android:id="@+id/ap_gratuity_label_value"
                        style="@style/TextViewAppearance.Body1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="5dp"
                        android:gravity="center"
                        android:text=""
                        app:typeface="robotoRegular" />

                    <SeekBar
                        android:id="@+id/ap_gratuity_seekbar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </LinearLayout>
            </LinearLayout>

            <CheckBox
                android:id="@+id/ap_charge_agreement_cb"
                style="@style/EditTextStyle.Standard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="I agree with future credit card or account charges"
                tools:ignore="HardcodedText" />

            <TextView
                android:id="@+id/ap_charges_agreement_tv"
                style="@style/TextViewAppearance.Body1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:linksClickable="true"
                android:text="@string/readchargesagreement" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/ap_action_btn_wrap"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#ffefefef"
        android:orientation="vertical" >
    </LinearLayout>
    <!-- <Button -->
    <!-- android:id="@+id/ap_action_button" -->
    <!-- android:layout_width="match_parent" -->
    <!-- android:layout_height="70dp" -->
    <!-- android:layout_alignParentBottom="true" -->
    <!-- android:layout_margin="10dp" -->
    <!-- android:background="@drawable/round_corners_shape_00428_selector" -->
    <!-- android:padding="10dp" -->
    <!-- android:textColor="@color/white" -->
    <!-- android:textSize="24sp" /> -->

</RelativeLayout>

ToolbarLayout 类

public class ToolbarLayout extends RelativeLayout {

    private Toolbar toolbar;
    private IconView toolbarLeftLogo;
    private ImageView toolbarTitleIcon;
    private TextView toolbarTitle;
    private Context context;

    public ToolbarLayout(Context context) {
        super(context);
        this.context = context;
        if (!isInEditMode()) {
            init();
        }
    }

    public ToolbarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        if (!isInEditMode()) {
            init();
        }
    }

    public ToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        if (!isInEditMode()) {
            init();
        }
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        RelativeLayout rootView = (RelativeLayout) inflater.inflate(R.layout.toolbar_layout, this);

        toolbar = (Toolbar) rootView.findViewById(R.id.general_toolbar);
        toolbarLeftLogo = (IconView) rootView.findViewById(R.id.general_toolbar_left_logo);
        toolbarTitleIcon = (ImageView) rootView.findViewById(R.id.general_toolbar_title_icon);
        toolbarTitle = (TextView) rootView.findViewById(R.id.general_toolbar_title);
        toolbarLeftLogo.setIcon("company_logo.png");
    }

    public void setActionBarTitle(String title) {
        if (title == null) {
            toolbarTitleIcon.setVisibility(View.VISIBLE);
            toolbarTitle.setVisibility(View.GONE);
            toolbarLeftLogo.setVisibility(View.GONE);
        } else {
            toolbarTitleIcon.setVisibility(View.GONE);
            toolbarTitle.setVisibility(View.VISIBLE);
            toolbarTitle.setText(title);
            toolbarLeftLogo.setVisibility(View.VISIBLE);
        }
    }

    public void setToolbarTitleText(String title) {
        if (title != null) {
            toolbarTitle.setText(title);
        }
    }

}

由 ToolbarLayout 类扩展的toolbar_layout XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/general_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffffff"
        android:minHeight="?attr/actionBarSize"
        android:visibility="visible" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp" >

            <com.l.mapprototype.view.IconView
                android:id="@+id/general_toolbar_left_logo"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:visibility="gone"
                tools:ignore="RtlHardcoded" />

            <ImageView
                android:id="@+id/general_toolbar_title_icon"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:padding="10dp"
                android:src="@drawable/company_title_icon"
                tools:ignore="ContentDescription" />

            <com.l.mapprototype.font.RobotoTextView
                android:id="@+id/general_toolbar_title"
                style="@style/TextViewAppearance.Headline1"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:layout_toRightOf="@+id/general_toolbar_left_logo"
                android:gravity="center_vertical|center_horizontal"
                android:padding="10dp"
                android:visibility="gone" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

</RelativeLayout>

【问题讨论】:

  • 试过运行hierarchyviewer ?
  • 顺便说一句,为什么ToolbarLayout 没有孩子?有什么原因吗?
  • 我没有尝试过 HeirarchyViewer,并且 ToolbarLayout 没有任何子项,因为我将它用作单个组件。 ToolbarLayout 类扩展了帖子“toolbar_layout”底部列出的布局。
  • no no no,在里面添加children,就好像它是一个“正常”的RelativeLayout一样,因为它是 RealtiveLayout,不是吗?
  • 它是一个RelativeLayout,但我不需要它来保存任何其他组件。我只需要它成为父布局下的一个组件,就像其他所有组件一样。我拥有的课程膨胀了自己的一组项目。所有这些都正确显示,因为它们在 ToolbarLayout 类中。

标签: java android xml android-layout view


【解决方案1】:

我设法让一切正常工作。这是我在 pskink 的帮助下得出的解决方案。

ToolbarLayout 类

public class ToolbarLayout extends RelativeLayout {

    private Toolbar toolbar;
    private IconView toolbarLeftLogo;
    private ImageView toolbarTitleIcon;
    private TextView toolbarTitle;
    private Context context;

    public ToolbarLayout(Context context) {
        super(context);
        this.context = context;
        if (!isInEditMode()) {}
    }

    public ToolbarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        if (!isInEditMode()) {}
    }

    public ToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        if (!isInEditMode()) {}
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        this.toolbar = (Toolbar) findViewById(R.id.general_toolbar);
        this.toolbarLeftLogo = (IconView) findViewById(R.id.general_toolbar_left_logo);
        this.toolbarTitleIcon = (ImageView) findViewById(R.id.general_toolbar_title_icon);
        this.toolbarTitle = (TextView) findViewById(R.id.general_toolbar_title);

        toolbarLeftLogo.setIcon("company_logo.png");
    }

    public void setActionBarTitle(String title) {
        if (title == null) {
            toolbarTitleIcon.setVisibility(View.VISIBLE);
            toolbarTitle.setVisibility(View.GONE);
            toolbarLeftLogo.setVisibility(View.GONE);
        } else {
            toolbarTitleIcon.setVisibility(View.GONE);
            toolbarTitle.setVisibility(View.VISIBLE);
            toolbarTitle.setText(title);
            toolbarLeftLogo.setVisibility(View.VISIBLE);
        }
    }

    public void setToolbarTitleText(String title) {
        if (title != null) {
            toolbarTitle.setText(title);
        }
    }
}

工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<com.l.mapprototype.view.ToolbarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/general_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffffff"
        android:minHeight="?attr/actionBarSize"
        android:visibility="visible" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp" >

            <com.l.mapprototype.view.IconView
                android:id="@+id/general_toolbar_left_logo"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:visibility="gone"
                tools:ignore="RtlHardcoded" />

            <ImageView
                android:id="@+id/general_toolbar_title_icon"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:padding="10dp"
                android:src="@drawable/company_title_icon"
                tools:ignore="ContentDescription" />

            <com.l.mapprototype.font.RobotoTextView
                android:id="@+id/general_toolbar_title"
                style="@style/TextViewAppearance.Headline1"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:layout_toRightOf="@+id/general_toolbar_left_logo"
                android:gravity="center_vertical|center_horizontal"
                android:padding="10dp"
                android:visibility="gone" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

</com.l.mapprototype.view.ToolbarLayout>

实施

<include
    android:id="@+id/primary_toolbar_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/toolbar_layout" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多