如何在可展开的文本视图中显示按钮

时间:2017-09-06 13:05:06

标签: android

在我的活动中我有一个带有多个滚动视图的线性视图,问题是我想要一个按钮到文本右侧的apear即将展开,下面是我使用的可扩展文本视图的代码android:src中我的drawables中的图像没有发生。

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

1 个答案:

答案 0 :(得分:0)

试试这段代码:

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

要添加自己的自定义图像,请在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);

    }
}

查看Documentation以获取更多帮助。