0

我正忙于一个水平滚动视图,里面有一个文本视图。我无法让 textview 能够在没有任何空间的情况下从一侧滑到另一侧。

如果我只是添加 textview 它不能滑动。

基本上我想要的是一个水平滚动视图,中间有一个文本视图,可以向左或向右滚动但不能过度滚动(仍然可见)

eg:
 |     Text     | 
 |Text          | 
 |          Text| 

But not 
 |ext           |
 |            Te|

我当前的布局如下所示:

<HorizontalScrollView
    android:layout_width="250dp"
    android:layout_height="35dp"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="36dp" >

        <Space
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:background="#808080" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEST"
            android:textSize="30dp" />

        <Space
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:background="#808080" />
    </LinearLayout>
</HorizontalScrollView>
4

1 回答 1

1

我实际上认为你的说法大部分是正确的。但是我认为您想要做的是在运行时获取屏幕分辨率,然后使用包装内容的文本视图,获取其运行时分辨率,然后将左侧空间和右侧空间的宽度(在运行时)设置为(ScreenResWidth-TextViewWidth)。

这意味着您需要为 TextView 和两个 Spaces 提供一个 ID,以便您可以从 Java 代码中访问它们。

采用:

android:gravity="center_horizontal"

在您的文本视图中居中文本。

采用:

textview.getWidth();

在您的 java 代码中获取 textview 的后布局宽度(以像素为单位)。

采用:

leftOrRightView.layout(int left, int top, int right, int bottom); 

使用 top、left、right 和 bottom 参数设置左右空间的位置和大小。

使用以下命令在 XML 文件中提供您的视图 ID:

android:id="@+id/myText"
android:id="@+id/myLeftSpace"
android:id="@+id/myRightSpace"
于 2013-02-11T14:16:22.250 回答