Trong ứng dụng tôi đang làm việc, tôi cần tạo một LinearLayout động. Trong trường hợp này, lệnh
ll.setClickable(true);
không hoạt động như phải làm. Mặc dù tôi có thể bỏ lỡ điều gì đó, nhưng tôi đã quản lý để khai thác setOnTouchListener để có được kết quả tương tự và tôi gửi mã trong trường hợp bất kỳ ai có nhu cầu giống nhau.
Đoạn mã sau tạo một LinearLayout với hai chế độ xem văn bản và các góc tròn, thay đổi màu sắc khi được nhấn.
Đầu tiên, tạo hai tệp xml trong thư mục có thể vẽ, một tệp ở trạng thái bình thường và một tệp cho trạng thái tuyến tính được nhấn.
Xml trạng thái bình thường (drawable / round_edges_normal.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="#FFFFFF" />
<corners android:radius="7dp" />
<padding android:left="5dip" android:top="5dip" android:right="5dip" android:bottom="5dip" />
</shape>
</item>
<item android:bottom="3px">
<shape android:shape="rectangle">
<solid android:color="#F1F1F1" />
<corners android:radius="7dp" />
</shape>
</item>
</layer-list>
Trạng thái được nén xml (drawable / round_edges_pressed.xml). Điểm khác biệt duy nhất là màu ...
<?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="#FFFFFF" />
<corners android:radius="7dp" />
<padding android:left="5dip" android:top="5dip" android:right="5dip" android:bottom="5dip" />
</shape>
</item>
<item android:bottom="3px">
<shape android:shape="rectangle">
<solid android:color="#add8e6" />
<corners android:radius="7dp" />
</shape>
</item>
</layer-list>
Sau đó, đoạn mã sau thực hiện công việc
Biến toàn cục:
public int layoutpressed = -1;
Trong onCreate():
// Create some textviews to put into the linear layout...
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setText("First Line");
tv2.setText("Second Line");
// LinearLayout definition and some layout properties...
final LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
// it is supposed that the linear layout will be in a table.
// if this is not the case for you change next line appropriately...
ll.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ll.setBackgroundResource(R.drawable.rounded_edges_normal);
ll.addView(tv1);
ll.addView(tv2);
ll.setPadding(10, 10, 10, 10);
// Now define the three button cases
ll.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_DOWN){
ll.setBackgroundResource(R.drawable.rounded_edges_pressed);
ll.setPadding(10, 10, 10, 10);
layoutpressed = arg0.getId();
}
else if (arg1.getAction()== MotionEvent.ACTION_UP){
ll.setBackgroundResource(R.drawable.rounded_edges_normal);
ll.setPadding(10, 10, 10, 10);
if(layoutpressed == arg0.getId()){
// ...........................................................................
// Code to execute when LinearLayout is pressed...
// ...........................................................................
}
}
else{
ll.setBackgroundResource(R.drawable.rounded_edges_showtmimata);
ll.setPadding(10, 10, 10, 10);
layoutpressed = -1;
}
return true;
}
});