【问题标题】:weights inside a scrollview [closed]滚动视图中的权重[关闭]
【发布时间】:2019-05-25 16:06:10
【问题描述】:

在我的应用程序中,我试图创建一个滚动视图,它有 3 个填充整个屏幕的大按钮。所以每个按钮的大小都是屏幕的 1/3。我怎样才能做到这一点? 当我使用权重时,它不起作用。

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imageView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/header_layout" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/main_btn_call" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/main_btn_unlock" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/main_btn_push" />

        </TableRow>
    </TableLayout>

</ScrollView>

【问题讨论】:

  • 您用作按钮的图像的确切尺寸是多少?
  • 为什么是尺码表?它们大约是 150dp。
  • 请定义“它们不起作用”。不要忘记您需要提供minimal reproducible example 才能让您的问题成为话题。

标签: android scrollview


【解决方案1】:

唯一的变化是在您的 XML 中添加以下属性:

android:fillViewport="true"

你的滚动视图宽度和高度应该是match_parent

android:layout_width="match_parent"
android:layout_height="match_parent"

【讨论】:

    【解决方案2】:

    您正在使用TableLayout,这是一个方向设置为verticalLinearLayout - 到目前为止一切正常,您在layout_height 属性上使用权重。

    我看到的问题是您在ScrollViewTableLayout 中都使用了wrap_content。权重值允许您的视图扩展以填充可用空间,但您的父级将内容包装为 0 + 0 + 0。如果您将两个父级元素的高度更改为 match_parent,您的权重值将有扩展空间

    【讨论】:

    • 这对我不起作用..
    • 在什么方面不起作用?
    • 它没有改变任何东西。
    • 你没有在你的问题中描述原来的问题是什么(显示什么以及这个“不工作”的方式是什么)。
    • 问题是图像没有根据权重自行调整大小,它们的行为就像那里没有权重一样。只是将一个堆叠在另一个上,而不是填满屏幕。
    【解决方案3】:

    我想用代码做到这一点:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView incallImage = (ImageView) findViewById(R.id.incall_image);
        final ImageView unlockImage = (ImageView) findViewById(R.id.unlock_image);
        final ImageView pushImage = (ImageView) findViewById(R.id.push_image);
        final ScrollView scrollViewMain = (ScrollView) findViewById(R.id.scroll_view_main);
        scrollViewMain.post(new Runnable() {
            @Override
            public void run() {
                scrollMainHeight = scrollViewMain.getHeight();
                LinearLayout.LayoutParams layoutParams  = new    
                         LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, scrollMainHeight / 3);
                incallImage.setLayoutParams(layoutParams);
                unlockImage.setLayoutParams(layoutParams);
                pushImage.setLayoutParams(layoutParams);
            }
        });
    }
    

    如果有人找到在 xml 中执行此操作的方法,那就太好了! 谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多