简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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