簡體   English   中英

XML問題中的自定義相對布局

[英]Custom Relative Layout in XML Issue

我遇到一個問題,即我創建的“自定義相對布局”類導致父XML布局中的其他組件消失或不顯示。

我可以添加一些內容到布局中,還是該類將阻止列出的第一個XML代碼中的所有其他布局/組件不顯示?

XML實施-ToolbarLayout

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

我設法使一切正常。 這是我在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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM