FrameLayout无法显示正确的宽度

时间:2019-04-29 14:23:22

标签: android width android-framelayout

我的布局如下(播放媒体文件):

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

请指出我做错了什么。谢谢!

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您的xml代码在这里工作正常……enter image description here

我怎么知道代码

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

其中可能有一些错误.....

答案 2 :(得分:0)

尝试使用

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

答案 3 :(得分: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)