Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

android - 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?

"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.

Edit: forgot to write how I tried to make the scrollable 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"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...