如何为已经有一些背景颜色的布局定义边框



我使用android:background="#ffffff"创建了一个白色背景的登录页面
现在我想给它添加一个边框。
为此,我创建了一个名为border_green.xml的.xml文件,但是我卡住了。
我正在努力在我的布局中调用border_green.xml,因为我已经为我的活动布局提供了背景,即:白色。

这里是activity.xml和border_green.xml。
请告诉我如何实现在border_green.xml中指定的背景色为白色和边框。

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"   
android:background="#ffffff"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
 >
<LinearLayout
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"        
    android:gravity="center_vertical|center_horizontal" >
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/project_name"
        android:background="@drawable/custom_bg_1"
        style="@style/pageTitle"
         />
</LinearLayout>
<LinearLayout
    android:id="@+id/ll2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"         
    android:gravity="center_vertical|center_horizontal" >

    <ScrollView
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <LinearLayout
                android:id="@+id/email_login_form"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
                <AutoCompleteTextView
                    android:id="@+id/autotxtViewSPCode"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:hint="@string/prompt_spcode"
                    android:inputType="textCapCharacters"
                    android:maxLines="1"
                    android:singleLine="true"
                    style="@style/AutoCompleteTextViewAppTheme"
                     />
                <EditText
                    android:id="@+id/txtViewpassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:hint="@string/prompt_password"
                    android:imeActionLabel="@string/action_login"
                    android:imeOptions="actionUnspecified"
                    android:inputType="textPassword"
                    style="@style/EditTextAppTheme"
                    />
                <Button
                    android:id="@+id/btnLogin"                       
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:text="@string/action_login"
                    android:layout_gravity="center"
                    style="@style/ButtonAppTheme"
                    />
                <Button
                    android:id="@+id/btnSetting"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:background="#ff7e51c2"
                    android:text="@string/action_setting_in"
                    style="@style/ButtonAppTheme"
                    android:visibility="gone"  />
                <Button
                    android:id="@+id/btnExit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:background="#ff7e51c2"
                    android:text="@string/action_exit_in"
                    style="@style/ButtonAppTheme"
                    android:textStyle="bold"
                    android:visibility="gone" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>
<LinearLayout
    android:id="@+id/ll3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="bottom|center_horizontal" >
    <Button
        android:id="@+id/email_setting_in_button_temp"
        style="?android:textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="#ff7e51c2"
        android:text="@string/action_setting_in"
        android:textColor="#ffffffff"
        android:textStyle="bold"
        android:visibility="gone" />
</LinearLayout>

border_green.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="3dp"
        android:color="#008080" />
       <padding android:left="30dp" android:right="30dp" />
    <solid android:color="@android:color/transparent" />
</shape>

添加一个xml文件,例如background.xml,代码如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
      android:radius="2dp"
      android:topRightRadius="0dp"
      android:bottomRightRadius="0dp"
      android:bottomLeftRadius="0dp" />
  <stroke
      android:width="1dp"
      android:color="@android:color/white" />
</shape>

可以

如果你想布局为白色,绿色边框,然后替换行

android:background="#ffffff" with `android:background="@drawable/layoutshape"`

您的layoutshape.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="2dp"
        />
    <solid
        android:color="#FFFFFF"
        />
    <stroke
        android:width="1dp"
        android:color="#084F08"
        />
</shape>

试试这个:

<?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="#00CC00" /> 
        </shape>
      </item>   
        <item android:left="2dp" android:right="2dp"  android:top="2dp" android:bottom="2dp" >  
         <shape android:shape="rectangle"> 
          <solid android:color="#ffffff" />
        </shape>
       </item>    
     </layer-list>

最新更新