有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java 安卓 Linearlayout设置了两种颜色

嗨,我试着这样做,我有一个线性布局,以及如何在布局内设置两种颜色的背景。我想知道一半是白色,一半是黑色

我试过这个:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">
    <item 安卓:state_pressed="false">
        <layer-list>
            <item>
                <shape>
                    <corners 安卓:radius="5dip"/>
                    <gradient
                        安卓:endColor="#fff"
                        安卓:startColor="#000" />
                </shape>
            </item>
        </layer-list>
    </item>

</selector> 

这是我的xml代码

<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:id="@+id/frame"
    安卓:background="@color/background"
    tools:context="pl.eltegps.smokkomunikator.ui.fragment.TopBarFragment">

    <RelativeLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent">

        <LinearLayout
            安卓:id="@+id/lin"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:orientation="horizontal">

            <RelativeLayout
                安卓:layout_width="0dp"
                安卓:layout_height="wrap_content"
                安卓:id="@+id/rel11"
                安卓:layout_weight="0.5">

                <ImageView
                    安卓:id="@+id/iv"
                    安卓:layout_width="30dp"
                    安卓:layout_height="30dp"
                    安卓:layout_marginLeft="10dp"
                    安卓:layout_marginStart="10dp"
                    安卓:src="@drawable/profil" />

                <TextView
                    安卓:id="@+id/user"
                    安卓:layout_width="wrap_content"
                    安卓:layout_height="wrap_content"
                    安卓:layout_below="@+id/iv"
                    安卓:layout_marginLeft="10dp"
                    安卓:layout_marginStart="10dp"
                    安卓:clickable="true"
                    安卓:text="aaaaaaa"
                    安卓:textColor="@color/textColor" />

            </RelativeLayout>

            <RelativeLayout
                安卓:id="@+id/settings"
                安卓:layout_width="0dp"
                安卓:layout_height="match_parent"
                安卓:layout_weight="0.5">

                <TextView
                    安卓:id="@+id/currentLocation"
                    安卓:layout_width="wrap_content"
                    安卓:layout_height="match_parent"
                    安卓:gravity="left"
                    安卓:textColor="#fff" />

                <ImageView
                    安卓:layout_width="30dp"
                    安卓:layout_height="30dp"
                    安卓:antialias="true"
                    安卓:background="@drawable/menu"
                    安卓:layout_centerVertical="true"
                    安卓:layout_alignParentRight="true"
                    安卓:layout_alignParentEnd="true"
                    安卓:layout_marginRight="11dp"
                    安卓:layout_marginEnd="11dp" />

            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_below="@+id/lin"
            >

            <FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
                安卓:layout_width="match_parent"
                安卓:layout_height="match_parent"
                安卓:orientation="vertical">

                <View
                    安卓:id="@+id/view"
                    安卓:layout_width="match_parent"
                    安卓:layout_height="1dp"
                    安卓:layout_gravity="center"
                    安卓:background="@color/textColor" />

                <LinearLayout
                    安卓:id="@+id/a"
                    安卓:layout_width="wrap_content"
                    安卓:layout_height="wrap_content"
                    安卓:layout_gravity="center"
                    安卓:layout_marginBottom="2sp"
                    安卓:layout_marginTop="2dp"
                    安卓:background="@drawable/dummy"
                    安卓:orientation="horizontal"
                    安卓:padding="5dp">

                    <RelativeLayout
                        安卓:id="@+id/serverStatusWrapper"
                        安卓:layout_width="wrap_content"
                        安卓:layout_height="wrap_content"
                        安卓:layout_marginLeft="2dp"
                        安卓:background="@drawable/shadow_green"
                        安卓:padding="2dp">

                        <ImageView
                            安卓:id="@+id/serverStatus"
                            安卓:layout_width="wrap_content"
                            安卓:layout_height="wrap_content"
                            安卓:layout_centerInParent="true"
                            安卓:antialias="true"
                            安卓:src="@drawable/ic_settings_input_antenna_white_24dp" />

                    </RelativeLayout>

                    <RelativeLayout
                        安卓:id="@+id/locationStatusWrapper"
                        安卓:layout_width="wrap_content"
                        安卓:layout_height="wrap_content"
                        安卓:background="@drawable/shadow_red"
                        安卓:padding="2dp">

                        <ImageView
                            安卓:id="@+id/locationStatus"
                            安卓:layout_width="wrap_content"
                            安卓:layout_height="wrap_content"
                            安卓:layout_centerInParent="true"
                            安卓:antialias="true"
                            安卓:src="@drawable/ic_warning_white_24dp" />
                    </RelativeLayout>

                </LinearLayout>

            </FrameLayout>

        </LinearLayout>

    </RelativeLayout>

</FrameLayout>

笨蛋。xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">
    <solid 安卓:color="#ffffffff" />

    <stroke
        安卓:width="1dp"
        安卓:color="#ff000000" />

    <padding
        安卓:bottom="1dp"
        安卓:left="1dp"
        安卓:right="1dp"
        安卓:top="1dp" />

    <corners
        安卓:bottomLeftRadius="7dp"
        安卓:bottomRightRadius="7dp"
        安卓:topLeftRadius="7dp"
        安卓:topRightRadius="7dp" />
</shape>

我试着这样做:什么是我想要的背景色黑色


共 (3) 个答案

  1. # 1 楼答案

    遵循以下步骤:

    步骤1:创建一个background.xml文件,并将该文件放在可绘制文件夹中

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:bottom="20dp">
        <shape android:shape="rectangle" >
            <size android:height="100dp" />
            <solid android:color="#ffffff" />
        </shape>
    </item>
    
    <item android:top="300dp">
        <shape android:shape="rectangle" >
            <size android:height="10dp" />
            <solid android:color="#000000" />
        </shape>
    </item>
    

    第2步:在Main.xml布局文件中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keypad"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/background">
    

    输出:

    enter image description here

  2. # 2 楼答案

    每个视图组只能有一种背景色,但通过创建两个视图组元素,可以轻松实现所需的效果

    喜欢

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    
    <FrameLayout
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="1"
    
       android:background="#ff0000"/>
    
    <FrameLayout
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="1"
    
       android:background="#00ff00"/>
    

  3. # 3 楼答案

    试一试

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#ffffff" />
            </shape>
        </item>
    
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#000000" />
            </shape>
        </item>
    </layer-list>