Çalıştığım uygulamada dinamik olarak bir LinearLayout oluşturmam gerekiyor. Bu durumda komut
ll.setClickable(true);
olması gerektiği gibi çalışmıyor. Bir şeyleri gözden kaçırsam da, setOnTouchListener'ı kullanarak aynı sonucu elde etmeyi başardım ve herkesin aynı ihtiyaçları olması durumunda kodu gönderiyorum.
Aşağıdaki kod, iki metin görünümüne ve yuvarlak köşelere sahip bir LinearLayout oluşturur ve basıldığında rengi değiştirir.
İlk olarak, biri normal diğeri basılı doğrusal düzen durumu için olmak üzere, çekilebilir klasörde iki xml dosyası oluşturun.
Normal durum xml (çekilebilir / rounded_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>
Basılmış durum xml (drawable / rounded_edges_pressed.xml). Tek fark renkte ...
<?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>
Ardından aşağıdaki kod işi yapar
Küresel değişken:
public int layoutpressed = -1;
İçinde 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;
}
});