简体   繁体   English

TextView大小和滚动问题(Android)

[英]TextView size and scrolling issue (Android)

I just have a TextView in a ScrollView which can contain different text. 我只是在ScrollView中有一个TextView,它可以包含不同的文本。 The size of the text is always different too. 文本的大小也总是不同的。 The problem is that on different screens this TextView can be fully placed on screen or not. 问题在于此TextView可以在不同的屏幕上完全放置在屏幕上,也可以完全不放置在屏幕上。 I want to center the text vertically on screen. 我想在屏幕上垂直居中放置文本。 If the text is small, it works fine. 如果文本较小,则可以正常工作。 But if the text length is bigger than the screen can contain, it just loses the beginning of text and also the strange empty space after text appears. 但是,如果文本长度大于屏幕可以容纳的长度,则它只会丢失文本的开头,还会丢失文本出现后的奇怪空白区域。 How can I fix this? 我怎样才能解决这个问题?

The attribute gravity of textview just centers the text horizontally, but layout_gravity does that strange thing I described above. textview的属性gravity仅将文本水平layout_gravity ,但是layout_gravity却做了我上面描述的奇怪的事情。 If I remove layout_gravity , all the text looks good, but it's at the top of the scrollview =( 如果我删除layout_gravity ,所有文本看起来都不错,但是它在滚动视图的顶部=(

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_above="@id/llbuttons"
    android:layout_alignParentTop="true"
    android:layout_margin="15dp" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:textSize="36sp"
        android:textStyle="bold" />
</ScrollView>

There is no need of ScrollView you need to add 2 attributes in xml android:maxLines and android:scrollbars="vertical" 不需要ScrollView您需要在xml android:maxLinesandroid:scrollbars="vertical"添加2个属性

XML XML

<TextView
        android:id="@+id/babySitterDescTxtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp"
        android:maxLines="3"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:scrollbars="vertical"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&apos;s standard dummy text ever since the 1500s."
        android:textColor="@color/fragment_page_txt_color"
        android:typeface="sans" />

in JAVA JAVA中

babySitterDescTxtView = (TextView) findViewById(R.id.babySitterDescTxtView);
babySitterDescTxtView.setMovementMethod(new ScrollingMovementMethod());

TextView can scroll the text itself. TextView可以滚动文本本身。 You don't need to use ScrollView: 您不需要使用ScrollView:

<TextView 
    android:text="Some text..." 
    android:id="@+id/my_textview_id" 
    android:textSize="20sp"
    android:textColor="#FFF"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:gravity="center"  
    android:scrollbars="vertical">

Try this way,hope this will help you to solve your problem. 尝试这种方式,希望这将帮助您解决问题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:gravity="center">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="36sp"
            android:layout_gravity="center"
            android:textStyle="bold" />
    </ScrollView>
    <Button
        android:id="@+id/llbuttons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Button"/>
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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