Reputation: 907
I added a View
to layout programmatically to draw a horizontal line.
Below is java code.
// I want to add a view to ll
LinearLayout ll = (LinearLayout)findViewById(R.id.main);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
View view = new View(this);
ll.addView(view);
view.setLayoutParams(params);
// this method does not work.
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.division_line));
my division_line.xml
in /drawable is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#800400"
android:width="2dp"/>
</shape>
I tried to apply division_line.xml
to View
but it doesn't work. What method should I use?
Upvotes: 0
Views: 2291
Reputation: 146
view.setBackground(getResources().getDrawable(R.drawable.division_line)) instead of view.setBackgroundDrawable(...)
and keep in mind that first 2dp isn't much and you are adding a view with no layout properties so its size is 0px x 0px
you can do so in code: view.setLayoutParams(new AbsListView.LayoutParams(300, 300));
Upvotes: 2