【问题标题】:Read More button in androidandroid中的阅读更多按钮
【发布时间】:2016-04-15 04:12:54
【问题描述】:

我浏览了几个阅读更多按钮的问题,但我没有得到一种我正在寻找更多的解决方案。我只想显示布局的某些部分,并且只有当用户按下“阅读更多”按钮时才会显示布局的其余部分和下拉动画。这种东西:

【问题讨论】:

标签: android android-animation


【解决方案1】:

我无法为您提供您想要的确切代码,但我可以通过提供满足您要求的示例代码来帮助您,

这里我只描述更多/更少按钮功能的工作原理。 首先在你的activity_main.xml 文件中粘贴下面的代码。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/description_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="5"
            android:text="@string/desc_content" />

        <ImageButton
            android:id="@+id/show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/description_text"
            android:background="@drawable/arrow_down"
            android:clickable="true" />

        <View
            android:id="@+id/view1"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_below="@+id/description_text"
            android:layout_marginTop="5dp"
            android:layout_toLeftOf="@+id/show"
            android:background="#000" />

        <ImageButton
            android:id="@+id/hide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/description_text"
            android:background="@drawable/arrow_up"
            android:clickable="true"
            android:visibility="invisible" />
    </RelativeLayout>

</ScrollView>

MainActivity.java

package com.example.expand.textview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity {
 TextView descText;
 ImageButton show, hide;

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

  descText = (TextView) findViewById(R.id.description_text);
  show = (ImageButton) findViewById(R.id.show);
  show.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    System.out.println("Show button");
    show.setVisibility(View.INVISIBLE);
    hide.setVisibility(View.VISIBLE);
    descText.setMaxLines(Integer.MAX_VALUE);

   }
  });
  hide = (ImageButton) findViewById(R.id.hide);
  hide.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    System.out.println("Hide button");
    hide.setVisibility(View.INVISIBLE);
    show.setVisibility(View.VISIBLE);
    descText.setMaxLines(5);

   }
  });

 }

}

最后更改您的 string.xml 文件 字符串.xml

<resources>

    <string name="app_name">Expand TextView</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="desc_content">     Android powers hundreds of millions of mobile devices in more than 190 countries around the world. It\'s the largest installed base of any mobile platform and growing fast—every day another million users power up their Android devices for the first time and start looking for apps, games, and other digital content.

Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.
</string>

</resources>

并运行你的项目它会给你下面的输出。

【讨论】:

    猜你喜欢
    • 2015-06-22
    • 1970-01-01
    • 2020-09-23
    • 2021-08-22
    • 2021-12-29
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2019-07-25
    相关资源
    最近更新 更多