2016-04-15 39 views
6

我已經瀏覽了幾個閱讀更多的按鈕問題,但我沒有得到一種解決方案,我正在尋找更多。我只想顯示佈局的某些部分,只有當用戶按下Read more按鈕時纔會顯示下拉動畫的其餘佈局。這種東西: enter image description here在Android中閱讀更多按鈕

enter image description here

+0

的[添加 「查看更多」 在TextView的結束後3行]可能的複製(http://stackoverflow.com/questions/19675331/add-view-more-at-the -end-of-textview-after-3-lines) –

+0

可能是http://stackoverflow.com/questions/19675331/add-view-more-at-the-end-of-textview-after-3-行 –

+0

是的,但我沒有得到滿意的解決方案.. @Sushant的答案正是我一直在尋找 – mrnobody

回答

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文件 string.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> 

並運行項目它會給你下面的輸出。

normal view Expanded view

相關問題