Making a LinearLayout act like an Button

前端 未结 10 2054
无人及你
无人及你 2020-11-29 19:09

I have a LinearLayout that I\'ve styled to look like a button, and it contains a few text/ImageView elements. I would like to make the whole

相关标签:
10条回答
  • 2020-11-29 19:26

    I ran into this problem just now. You'll have to set the LinearLayout to clickable. You can either do this in the XML with

    android:clickable="true"
    

    Or in code with

    yourLinearLayout.setClickable(true);
    

    Cheers!

    0 讨论(0)
  • 2020-11-29 19:29

    If you want add the Android default background behavior to make a Layout acts like a "clikable" View, set on the targeted Layout:

    API 11+ (Pure Android):

    android:background="?android:attr/selectableItemBackground"
    

    API 7+ (Android + AppCompat Support Library):

    android:background="?attr/selectableItemBackground"
    

    Any API:

    android:background="@android:drawable/list_selector_background"
    

    Answers above still true but didn't help me for just add the default pressed and released UI state (like in a ListView for instance).

    0 讨论(0)
  • 2020-11-29 19:35

    In the application I am working I need to create a LinearLayout dynamically. In this case the command

    ll.setClickable(true);
    

    is not working as supposed to do. Although I may miss something, I managed to exploit setOnTouchListener to get the same result and I submit the code in case anyone has the same needs.

    The following code creates a LinearLayout with two textviews and round corners, changing color when pressed.

    First, create two xml files in drawable folder, one for normal and one for pressed linearlayout state.

    Normal state 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>
    

    Pressed state xml (drawable/rounded_edges_pressed.xml). The only difference is in the color...

    <?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>
    

    Then the following code does the job

    Global variable:

    public int layoutpressed = -1;
    

    In 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;
                    }
      });           
    
    0 讨论(0)
  • 2020-11-29 19:39

    Just set these attributes

    <LinearLayout 
        ...
        android:background="@android:drawable/btn_default" 
        android:clickable="true"
        android:focusable="true"
        android:onClick="onClick"
        >
    ...
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题