FrameLayout не отображает правильную ширину - PullRequest
0 голосов
/ 29 апреля 2019

У меня есть макет, как показано ниже (для воспроизведения медиа-файлов):

<?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 голосов
/ 30 апреля 2019

Спасибо за ответы, ребята. К сожалению, ни один из ответов не сработал. Но я исправил проблему.

По какой-то неизвестной причине проблема 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)
0 голосов
/ 29 апреля 2019

ваш xml-код работает нормально ...... enter image description here

покажи мне код

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

Там может быть какая-то ошибка .....

0 голосов
/ 29 апреля 2019

попробуйте использовать

shadowingView.setLayoutParams (новый FrameLayout.LayoutParams (FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));

0 голосов
/ 29 апреля 2019

вы пропустите атрибут 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

...