2011-06-15 109 views

回答

0

您只需要創建一個九個補丁的圖像並設置背景。

+0

我不想使用圖像,我想使用顏色 – Jovan 2011-06-15 21:03:16

+0

然後,您可以創建一個ShapeDrawable並更改筆觸顏色。 – CaseyB 2011-06-15 21:09:11

+0

可以請你提供我的代碼嗎?謝謝 – Jovan 2011-06-15 21:16:01

1

對於Android,LinearLayout佈局不帶邊框屬性。但是,您可以將LinearLayout的背景設置爲9補丁圖像。

只要將其鏈接回您的Java文件,您就可以修改您創建的xml文件上的任何內容。

例子: 如果您的XML文件是:

<?xml version="1.0" encoding="utf-8"?> 

<!-- note that the id is linearlayout. That's important, you want the id to be something you can use to identify that element. --> 
<LinearLayout` 
    xmlns:android="http://schemas.android.com/apk/res/android"` 
android:id="@+id/linearlayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
</LinearLayout> 

然後在你的Java文件,你不得不這樣做:如果你

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.whateverXMLFile); 

    //get the Linear Layout you're searching for 
    LinearLayout linLayout = (LinearLayout) findViewById(R.id.linearlayout); 

    /*background is your background image you want to replace with. 
    * You can use any that is in your drawable resource. 
    * Better to use a 9 sketch because it'll expand to fit your width/height, no matter 
    * how big or small your layout will be. 
    */ 
    linLayout.setBackgroundResource(R.drawable.background); 


    //if you want to set the background colour: 
    //will set it to RED, you can also specify a resource file you may have for it 
    linLayout.setBackground(Colour.RED); 
}