简体   繁体   中英

How to make button visible in expandable text view

in my activity I have a linear view with multiple scroll views in it, problem is I want a button to apear to the right of the text thats about to be expanded, bellow is my code for the expandable text view when i use android:src for the image in my drawables no happens though.

 <com.ms.square.android.expandabletextview.ExpandableTextView
                android:id="@+id/expandable_text_view1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"


                app:animDuration="200"
                app:maxCollapsedLines="1"

                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"

                    >

                    <TextView
                        android:id="@+id/expandable_text"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:textSize="18sp"


                        />

                    <ImageButton

                        android:id="@+id/expand_collapse"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_gravity="bottom|end"
                        android:background="@android:color/transparent"
                        />

                </LinearLayout>

            </com.ms.square.android.expandabletextview.ExpandableTextView>

Try this code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:padding="16dp">

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expandable_text_view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:animDuration="200"
        app:maxCollapsedLines="1">

        <TextView
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="8dp"
            android:textColor="@android:color/black"
            android:id="@+id/expandable_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp" />

        <ImageButton
            android:id="@+id/expand_collapse"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="bottom|end"
            android:background="@android:color/transparent" />

    </com.ms.square.android.expandabletextview.ExpandableTextView>
</LinearLayout>

To add your own custom image, add the following properties in your ExpandabaleTextView:

app:expandDrawable="@drawable/expand_image"
app:collapseDrawable="@drawable/collapse_image"

MainActivity:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.ms.square.android.expandabletextview.ExpandableTextView;

public class MainActivity extends AppCompatActivity {

    ExpandableTextView expandableTextView;
    String long_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        expandableTextView = (ExpandableTextView) findViewById(R.id.expandable_text_view1);
        expandableTextView.setText(long_text);

    }
}

Check out the Documentation for more help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM