我正在以编程方式从LinearLayout创建一个listview数组,我想制作圆角,所以我创建了一个像这样的可绘制资源
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="40dp"
/>
<padding
android:top="2dp"
android:bottom="2dp"
android:left="2dp"
android:right="2dp"/>
</shape>
LinearLayout的代码是这个
LinearLayout linearLayout= new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layout= new LinearLayout.LayoutParams(
200,
100
);
layout.setMargins(20,20,20,20);
linearLayout.setLayoutParams(layout);
linearLayout.setBackgroundColor(Color.BLUE);
linearLayout.setBackgroundResource(R.drawable.rounded);
linearLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("Test selected","");
v.setBackgroundColor(Color.RED);
return false;
}
});
问题是元素的形状不是圆形的。我的错在哪里?感谢每一个抱歉我的英语。
答案 0 :(得分:1)
使布局具有圆角:
您必须定义一个xml文件,其中包含在res / drawable中具有名称rounded.xml的圆角属性(您可以保存任何名称)
形状 的xmlns:机器人= “http://schemas.android.com/apk/res/android” 机器人:形状= “矩形” &GT;
<gradient
android:endColor="#000000"
android:startColor="#000000"/>
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
将您的活动定义为:
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearLayout= new LinearLayout(getApplicationContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
//Use framelayout.layoutparams not linearlayout.layoutparams
FrameLayout.LayoutParams layout= new FrameLayout.LayoutParams(200,100);
//Use setContentView to show this layout in your activity
setContentView(linearLayout,layout);
layout.setMargins(50,50,50,50);
linearLayout.setLayoutParams(layout);
linearLayout.setBackgroundColor(Color.BLUE);
linearLayout.setBackgroundResource(R.drawable.rounded);
linearLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("Test selected","");
v.setBackgroundColor(Color.RED);
return false;
}
});
我查了一下:
FrameLayout.LayoutParams layout= new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
布局的颜色在rounded.xml中设置为黑色
输出是: