BackgroundResource边框顶部和底部

时间:2013-07-08 10:24:17

标签: java android

其实我有这段代码:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid 
        android:color="#ffffff" />
    <stroke 
        android:width="0.5dp" 
        android:color="#000000" />
</shape>

它会在视图周围创建边框,但我只想在顶部和底部创建边框。

如何?

编辑:对不起,我忘了应该按状态按

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true"   
        android:drawable="@color/color" />
</selector>

2 个答案:

答案 0 :(得分:1)

试试这个

可绘制文件夹中的bkg.xml

 <?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="#FF0000" /> 
    </shape>
  </item>   
    <item android:bottom="5dp"   android:top="5dp" >  
     <shape android:shape="rectangle"> 
      <solid android:color="#000000" />
    </shape>
   </item>    
 </layer-list> 

根据您的要求更改颜色和笔触宽度。

图形编辑器的快照

enter image description here

编辑:评论中的问题

定义itembkg。 xml如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:drawable="@drawable/bkg" /> 
        // bkg.xml in drawable folder
        // drawable in pressed state 
    <item  android:state_focused="false" 
        android:drawable="@drawable/tvnormal" /> 
        // set a different drawable in normal state
</selector>

然后在您的视图中添加以下属性

android:background="@drawable/itembkg"

答案 1 :(得分:0)

试试这个......

  <?xml version="1.0" encoding="utf-8"?>
  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item > 
    <shape android:shape="rectangle">

       <padding android:top="1dp"
                android:bottom="1dp" />
       <solid android:color="#000000"/>


    </shape>
  </item>
  <item > 
    <shape android:shape="rectangle">

        <solid android:color="#ffffff"/>

    </shape>
 </item>
 </layer-list>