जिस एप्लिकेशन में मैं काम कर रहा हूं, मुझे गतिशील रूप से LinearLayout बनाने की आवश्यकता है। इस मामले में आदेश
ll.setClickable(true);
के रूप में काम करने वाला नहीं है। हालांकि मुझे कुछ याद आ सकता है, मैं एक ही परिणाम पाने के लिए setOnTouchListener का फायदा उठाने में कामयाब रहा और अगर किसी की भी ज़रूरतें हों, तो मैं कोड जमा करता हूं।
निम्न कोड दो पाठ और गोल कोनों के साथ एक रेखीयआउट बनाता है, जब दबाया जाता है तो रंग बदलता है।
सबसे पहले, ड्रा करने योग्य फ़ोल्डर में दो एक्सएमएल फाइलें बनाएं, एक सामान्य और एक दबाए गए लाइनरलेआउट राज्य के लिए।
सामान्य स्थिति xml (drawable / 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>
दबाया गया राज्य xml (drawable / rounded_edges_pressed.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="#add8e6" />
<corners android:radius="7dp" />
</shape>
</item>
</layer-list>
फिर निम्नलिखित कोड काम करता है
वैश्विक चर:
public int layoutpressed = -1;
इन 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;
}
});