Рисуемый дизайн XML не виден как фон кнопки в основном коде XML - PullRequest
0 голосов
/ 26 декабря 2018

Я пытаюсь создать макет, который имеет две кнопки, для создания этих кнопок я использовал в качестве фона нарисованные XML-файлы.В андроид-студии предварительный просмотр Android-студии показывает

enter image description here, эти кнопки не видны, но genymotion показывает эти кнопки.Результат запуска кода в виртуальном устройстве android genymotion

enter image description here

С другой стороны, когда я запускаю этот код на реальном устройстве, он не отображаетсяэти кнопки.

Результат выполнения кода на реальном устройстве Android

enter image description here

Код выводимого XML-файла: @ drawable / button_design

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/navHeadBack"/>
            <corners android:topRightRadius="130dp"
                android:bottomLeftRadius="130dp"
                android:topLeftRadius="130dp"
                android:bottomRightRadius="130dp"/>
            <stroke android:width="5dp" android:color="@color/navHeadBack" />
        </shape>
    </item>
</layer-list>

Код файла XML-проекта активности:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:weightSum="10"
    android:background="@color/commonColor"
    android:orientation="vertical"
    tools:context=".Activities.FirstPage">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5.9"
        android:layout_margin="10dp"
        android:background="@color/commonColor"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@mipmap/logo"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="4.1"
        android:layout_margin="10dp"
        android:gravity="center|bottom"
        android:orientation="vertical">
        <Button
            android:id="@+id/u_sign_in"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:elevation="20dp"
            android:text="@string/user_sign_in"
            android:textColor="@color/commonColor"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/button_design"
            android:textAllCaps="false"/>
        <Button
            android:id="@+id/u_sign_up"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:elevation="20dp"
            android:text="@string/user_sign_up"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="10dp"
            android:textColor="@color/commonColor"
            android:background="@drawable/button_design_up"
            android:textAllCaps="false" />

    </LinearLayout>


</LinearLayout>

Я хочу сделать видимым / отобразить нарисованный XML-дизайн на фоне этих кнопок.Как я могу это сделать?
ПРИМЕЧАНИЕ: в коде xml, когда я использовал android:enabled="true", кнопка становится невидимой, а когда я использовал android:enabled="false", она становится видимой.Но я не понимаю, почему.

Ответы [ 7 ]

0 голосов
/ 26 декабря 2018

Эй, вам нужно android:layout_height="0dp" оба макета при использовании android:layout_weight

<LinearLayout 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"
android:background="@color/commonColor"
android:orientation="vertical"
android:weightSum="10"
tools:context=".Activities.FirstPage">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:layout_weight="5.9"
    android:background="@color/commonColor"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/logo" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:layout_weight="4.1"
    android:gravity="center|bottom"
    android:orientation="vertical">

    <Button
        android:id="@+id/u_sign_in"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginBottom="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_design"
        android:elevation="20dp"
        android:text="@string/user_sign_in"
        android:textAllCaps="false"
        android:textColor="@color/commonColor" />

    <Button
        android:id="@+id/u_sign_up"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/button_design_up"
        android:elevation="20dp"
        android:text="@string/user_sign_up"
        android:textAllCaps="false"
        android:textColor="@color/commonColor" />

</LinearLayout>

0 голосов
/ 27 декабря 2018

В моем коде я попробовал приведенный ниже код для пользовательского фона. Попробуйте, надеюсь, он будет работать для вас

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="60dp" />

<solid android:color="color you want" />
</shape>
0 голосов
/ 26 декабря 2018

Использование constrainLayout решит эту проблему, а использование ConstrainLayout удалит вложенный макет и улучшит производительность.

Это пример кода, который делает этот дизайн с использованием макета с ограничениями -

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.constraint.Guideline
        android:id="@+id/gl_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.50"/>

    <android.support.v7.widget.AppCompatImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/gl_center"/>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_first"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Button 2"
        android:background="@drawable/bg_btn_first"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_second"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Button 2"
        android:background="@drawable/bg_btn_first"
        app:layout_constraintBottom_toTopOf="@+id/btn_first"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

файл для рисования

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <corners android:radius="16dp"/>
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>
</layer-list>
0 голосов
/ 26 декабря 2018

Вы используете android:layout_height="match_parent" вместе с весами.

Вам нужно установить layout_height на 0dp, чтобы веса работали.

0 голосов
/ 26 декабря 2018
Do Like This:-

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@color/commonColor"
    android:orientation="vertical"
    tools:context=".Activities.FirstPage">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_contant"
            android:src="@mipmap/logo"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center|bottom"
        android:orientation="vertical">
        <Button
            android:id="@+id/u_sign_in"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:elevation="20dp"
            android:text="@string/user_sign_in"
            android:textColor="@color/commonColor"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/button_design"
            android:textAllCaps="false"/>
        <Button
            android:id="@+id/u_sign_up"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:elevation="20dp"
            android:text="@string/user_sign_up"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="10dp"
            android:textColor="@color/commonColor"
            android:background="@drawable/button_design_up"
            android:textAllCaps="false" />

    </LinearLayout>


</LinearLayout>
0 голосов
/ 26 декабря 2018

Попробуйте использовать RelativeLayout, чтобы получить imageview layout_alignParentTop = "true" и сделать кнопку layout_alignParentBottom = "true" и удалить layout_weight.

0 голосов
/ 26 декабря 2018
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@color/commonColor"
    android:orientation="vertical"
    tools:context=".Activities.FirstPage">

      <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/logo"/>

     <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">
        <Button
            android:id="@+id/u_sign_in"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:elevation="20dp"
            android:text="@string/user_sign_in"
            android:textColor="@color/commonColor"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/button_design"
            android:textAllCaps="false"/>
        <Button
            android:id="@+id/u_sign_up"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:elevation="20dp"
            android:text="@string/user_sign_up"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="10dp"
            android:textColor="@color/commonColor"
            android:background="@drawable/button_design_up"
            android:textAllCaps="false" />    
    </LinearLayout>      
</LinearLayout>

Я удалил layout_weight и сделал layout_height="wrap_content" для дочерних представлений LinearLayout.Не могли бы вы попробовать макет выше?Вы должны исправить некоторые измерения.Надеюсь, это поможет


Я написал простой макет.Вы можете использовать и редактировать его, чтобы сделать так, как вы ожидаете.С другой стороны, я предполагаю, что кнопки (в вашем коде) видны в пользовательском интерфейсе, но цвет фона и текста белый.Не могли бы вы проверить это

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@android:drawable/btn_star" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff0000" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button 1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button 2" />
</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...