简体   繁体   English

Android:如何给 LinearLayout 一个最大宽度?

[英]Android: How to give a LinearLayout a maximum width?

I am designing a layout for my Android app.我正在为我的 Android 应用程序设计布局。

I want a LinearLayout within a ScrollView to stretch.我希望 ScrollView 中的LinearLayout进行拉伸。 I achieved this with layout_width:match_parent.我通过 layout_width:match_parent 实现了这一点。 However, I don't want to stretch it beyond 500dp.但是,我不想将其拉伸到 500dp 以上。

The layout should look something like this:布局应如下所示:

https://imgur.com/a/iwzMA49 https://imgur.com/a/iwzMA49

So far I've tried android:maxWidth and app:layout_constraintWidth_max (with layout_width="0dp" and layout_width="match_parent") .到目前为止,我已经尝试过 android:maxWidth 和 app:layout_constraintWidth_max (with layout_width="0dp" and layout_width="match_parent")

How does on achieve this layout without extra files for certain screen sizes?对于某些屏幕尺寸,如何在没有额外文件的情况下实现这种布局?

You could set the width of the linearLayout programmatically in onCreate() for each screen mode:您可以在 onCreate() 中为每种屏幕模式以编程方式设置 linearLayout 的宽度:

LinearLayout linearLayout = findViewById(R.id.ll);

        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            linearLayout.setLayoutParams(new LinearLayout.LayoutParams(500, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        else {
            linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM