FrameLayout没有显示正确的宽度

问题描述 投票:0回答:4

我有一个像下面的布局(播放媒体文件):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/mediabar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnPlay"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="0.2"
            android:text="P">

        </Button>

        <SeekBar
            android:id="@+id/sbProgress"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"/>

        <TextView
            android:id="@+id/txtTimeleft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00:00:00"
            android:textSize="13sp"
            android:paddingRight="10dp"/>

    </LinearLayout>

</LinearLayout>

enter image description here

我使用一个名为CustomView的类,它继承自FrameLayout以包含上面的布局。

public class CustomView extends FrameLayout

然后我将在启动时将以上布局添加到CustomView:

private void initItems(){
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = null;
    rowView = inflater.inflate(R.layout.shadow_layout,null,false);

    btnPlay = rowView.findViewById(R.id.btnPlay);
    seekBar = rowView.findViewById(R.id.sbProgress);
    tvTime = rowView.findViewById(R.id.txtTimeleft);
    //scriptFrame = (LinearLayout) rowView.findViewById(R.id.scriptframe);

    this.addView(rowView);
    setUpFont();
}

最后,在我的活动中,我将创建并将上面的类添加到我的视图中,如下所示:

shadowingView = new CustomView(context);
shadowingView.setLayoutParams(
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));

scrollView.addView(shadowingView);

但结果不是我的预期,布局不是MATCH_PARENT(播放按钮的文字也不显示):

enter image description here

请指出我做错的地方。谢谢!

android width android-framelayout
4个回答
0
投票

你错过了android:weightSum中的linearlayout属性

<LinearLayout
        //put the weightSum here 
        android:id="@+id/mediabar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
        android:id="@+id/btnPlay"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.2"
        android:text="P">

    </Button>

    <SeekBar
        android:id="@+id/sbProgress"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/txtTimeleft"
        android:layout_width="0dip"
         android:layout_weight="0.2"
        android:layout_height="wrap_content"
        android:text="00:00:00"
        android:textSize="13sp"
        android:paddingRight="10dp"/>

    </LinearLayout>

结果如下:image


0
投票

你的xml代码在这里工作得很好...... enter image description here

给我看代码

<FrameLayout
......
......
/>

它可能有一些错误.....


0
投票

尝试使用

shadowingView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT));


0
投票

谢谢你的回答。不幸的是,没有一个答案奏效。但我已经解决了这个问题。

由于一些未知的原因,scrollview是问题,我所要做的就是从FrameLayout改为ScrollView

public class CustomView extends ScrollView

和这里 :

shadowingView = new CustomView(context);
shadowingView.setLayoutParams(
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));

// scrollView.addView(shadowingView);  REMOVE THIS LINE 
// topParent.addView(scrollView); AND THIS LINE 

topParent.addView(shadowingView)
© www.soinside.com 2019 - 2024. All rights reserved.