简体   繁体   English

使用layout_weight在textview中自动水平滚动

[英]Automatic horizontal scroll in textview with layout_weight

I have been looking at other threads but I couldn't find an answer so here comes my question: 我一直在寻找其他线程,但找不到答案,因此出现了我的问题:

Is it possible to create a automatically horizontal scrolling TextView with a button to the right of it using layout_weight? 是否可以使用layout_weight创建一个带有水平按钮的自动水平滚动TextView?

"My incredibly long search text here" "The button" “我的搜索文本长得令人难以置信”“按钮”

I have tried to make a scrollable textview with "fill_parent" instead of 0dp and layout_weight as well but then the entire text takes up the "row" (obviously since it is fill_parent) and the button is not shown AND the text didn't scroll horizontally even then when I ran it in the android virtual device. 我试图用“ fill_parent”而不是0dp和layout_weight来制作一个可滚动的文本视图,但是整个文本占据了“行”(显然是因为它是fill_parent),并且按钮没有显示并且文本没有滚动即使在我在android虚拟设备中运行它时,它也会保持水平。

Edit: forgot to write how I tried to make the scrollable textview 编辑:忘记写我如何尝试使可滚动的textview

<TextView
 android:singleLine="true"
 android:scrollHorizontally="true"
 android:ellipsize="marquee"
 android:marqueeRepeatLimit="marquee_forever"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:id="@+id/searchResult"
 android:focusable="true"
 android:focusableInTouchMode="true"/>

Of course it's possible. 当然可以。 Something like this will do: 这样的事情会做:

<LinearLayout
    android:layout_widht="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0px"
        android:layout_weight="7"
        .../>

    <Button
        android:layout_width="0px"
        android:layout_weight="3"
        ... />

</LinearLayout>

Key here is make the width zero so that there's all horizontal space remaining when the weight mechanism assigns all remaining space to linear layout components in relation to their layout weight. 此处的关键是将宽度设置为零,以便在权重机制将所有剩余空间分配给相对于其布局权重的线性布局组件时,将保留所有水平空间。

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

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