我想用Java代码绘制不同颜色的linearlayout边框,而不是使用xml文件
如果可能的话?请给我代码。
由于
答案 0 :(得分:1)
对于Android,LinearLayout布局没有border属性。但是,您可以将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);
}
答案 1 :(得分:0)
您只需要创建九个补丁图像并设置背景。