【问题标题】:ViewPager in android is taking full screen?android中的ViewPager正在全屏显示?
【发布时间】:2020-03-13 00:05:40
【问题描述】:

在我的 xml 文件中,我有一个线性布局,其中有一个 ViewPager 用于显示图像,另一个线性布局包含用于选择图像的上一个和下一个按钮。我的 xml 如下所示:

 <?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="match_parent" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/goto_first"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="first" >
            </Button>

            <Button
                android:id="@+id/goto_last"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="last" >
            </Button>
        </LinearLayout>

    </LinearLayout>

我的问题是 ViewPager 正在全屏显示,并且带有上一个下一个按钮的 Linearlayout 没有显示,因为没有剩余空间来绘制此 LinearLayout。

我正在使用 Fragments 用视图填充 ViewPager。进入 ViewPager 的片段视图的 xml 文件是:

<ImageView
    android:id="@+id/ItemImage"
    android:layout_width="320dp"
    android:layout_height="180dp" />

<TextView
    android:id="@+id/ItemText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|bottom"
    android:layout_marginBottom="5dp"
    android:textColor="#ffffffff"
    android:textStyle="bold" />
</FrameLayout>

渲染后我得到的输出是这样的:
1.image(覆盖45%屏幕高度)
2.空白空间(覆盖45%屏幕高度)
3.Textview(覆盖其余10%屏幕高度)

我想要的输出是:
1.image(覆盖45%的屏幕高度)
2.Textview(覆盖10%屏幕高度)
3.LinearLayout for Buttons(覆盖剩下的 45% 屏幕高度)

【问题讨论】:

  • 我得到了答案 [link]stackoverflow.com/questions/8532307/…
  • 那么你今天学到了什么?在问问题之前先在这个网站上进行一些谷歌搜索和搜索:)
  • @Miral 我做到了,但不知何故它没有出现在搜索结果中,突然在发布这个问题后我找到了问题的链接。无论如何,从下次开始我会花更多时间搜索跨度>
  • 我认为你的问题是由于 android:layout_gravity="left|bottom"
  • @Sherya 不,这不是问题,因为我删除了它但仍然得到相同的结果。我要为我的问题添加答案。等待几秒钟

标签: android android-layout android-viewpager


【解决方案1】:

使用相对布局。使用alignParentBottom="true" 将您的按钮添加到底部。然后用layout_above="@id/buttons"添加ViewPager

【讨论】:

    【解决方案2】:

    把那些给viewpager:

     android:layout_height="0dp"
     android:layout_weight="1"
    

    【讨论】:

    • android:layout_weight="1" 不适用于 ViewPager
    • @FaizanMubasher 虽然这是一个旧答案,但我想指出它对我有用,“androidx.viewpager.widget.ViewPager”和“LinearLayout”中的其他元素
    【解决方案3】:

    viewpagers 的问题是他们不知道大小,因为每个片段可能是不同的大小。您必须手动设置 viewpager,而不是 wrap_content。我建议为外部线性布局中的所有项目设置权重。

    【讨论】:

    • 这个解决方案的一个例子是@zhumingvictor answer。它对我有用
    【解决方案4】:

    只需使用RelativeLayout 并使用像`

    这样的属性

    android:layout_below

    直到你达到你想要的位置。在这些情况下,相对布局是您的朋友。

    【讨论】:

      【解决方案5】:

      只需执行以下操作即可为下方或上方的其他视图腾出空间:

      < android.support.v4.view.ViewPager **android:layout_width**="match_parent" **android:layout_height**="match_parent" **android:layout_weight**="1"/>
      

      此答案取自 Jave 根据以下链接给出的答案发表的评论。

      链接:“Android ViewPager dimension

      【讨论】:

        【解决方案6】:
        public class WrapViewPager extends ViewPager {
        
            public WrapViewPager(Context context) {
                super(context);
            }
        
            public WrapViewPager(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
        
            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        
                int height = 0;
                for (int i = 0; i < getChildCount(); i++) {
                    View child = getChildAt(i);
                    child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                    int h = child.getMeasuredHeight();
                    if (h > height) height = h;
                }
        
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        
        }
        

        【讨论】:

          【解决方案7】:

          就我而言,这段代码有效:

          <androidx.viewpager.widget.ViewPager
              android:id="@+id/myviewpager"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1" />
          

          【讨论】:

            【解决方案8】:

            我在实施 ap 时遇到了这个问题。 要将图像填充到 ViewPager 中,请使用 RelativeLayout 而不是 FrameLayout

            在我的情况下 FrameLayout 占用了超过 30% 的空白空间。当我使用 RelativeLayout 时,空间已经消失了。

            像这样

            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent”>
            <ImageView
                    android:id="@+id/itemImagePager"
                    android:layout_width="match_parent"
                    android:layout_height=“match_parent"
                    android:scaleType="fitCenter" />
            <ProgressBar
                    android:id="@+id/loading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true”/>
            </RelativeLayout>
            

            希望它对你有用。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多