线性布局水平方向推动视图超出屏幕

3

我想把三个视图水平放在一起:TextView - SeekBar - TextView。这是我的示意图:0:00 ------0------ 0:00。

我希望SeekBar可以弹性伸缩,并且两侧有两个计数器,但最后一个textView没有显示出来。我还没有学习过Android中的布局相关知识。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/player_background"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/Progress01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textIsSelectable="false"
        android:text="@string/progress" 
        android:textColor="@color/player_duration_progress"/>

    <SeekBar
        android:id="@+id/SeekBar01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        />

    <TextView
        android:id="@+id/Duration01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp" 
        android:textIsSelectable="false"
        android:text="@string/duration"
        android:textColor="@color/player_duration_progress"/>            
</LinearLayout>
</LinearLayout>

也许在这里使用LinearLayout并不是最合适的选择?
3个回答

2

我建议您使用RelativeLayout代替:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/Progress01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:text="progress"
        android:textColor="#000000"
        android:textIsSelectable="false"
        android:textSize="15sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/Duration01"
        android:layout_toRightOf="@+id/Progress01"
        android:orientation="horizontal" >

        <SeekBar
            android:id="@+id/SeekBar01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:id="@+id/Duration01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:text="duration"
        android:textColor="#000000"
        android:textIsSelectable="false"
        android:textSize="15sp" />

</RelativeLayout>

那几乎完美了!小问题是现在SeekBar浮在TextViews的顶部,一直延伸到两侧。我能修复吗? - Toydor
嗯...我在真实设备上仔细检查过了,它完全正常:TextView - SeekBar - TextView - 就像你想要的那样。你确定你没有在中间放置任何东西或更改了layout_toLeftOf或toRightOf吗?- 这些是布局的关键特性。 - gunar
我明白了...你不想扯淡...误读了。我会修改回复并回复你。 - gunar
把SeekBar放到另一个ViewGroup中,该ViewGroup应该延伸到文本视图,并在ViewGroup内将宽度设置为“wrap_content”。这样就可以了。 - gunar

1
问题在这里:

<SeekBar
        android:id="@+id/SeekBar01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        />

你将SeekBar的宽度设置为fill_parent,没有留下任何空间给其他TextView。

0

将SeekBar的LayoutWidth更改为wrap_content。

请查看下面:

<SeekBar
    android:id="@+id/SeekBar01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接