代码之家  ›  专栏  ›  技术社区  ›  L.Butz Argh

如何让应用程序自动适应不同的屏幕宽度

  •  0
  • L.Butz Argh  · 技术社区  · 6 年前

    我做了一些调查 this article ,但无法找到任何解决方案。

    我的问题: 我有一个标题布局,包括在我的所有屏幕。根据屏幕的宽度,我想显示三种不同的页眉布局之一。侧注;我的应用程序只支持纵向模式。

    我的布局是:

    • [图像][按钮][文本][按钮][文本]-->用于大屏幕
    • [图像][按钮][文本][按钮]-->用于中等屏幕
    • [图像][按钮][按钮]-->用于小屏幕

    它们应该由应用程序根据屏幕宽度自动选择。我想用编程的方式来解决这个问题,因为我在每一个活动中都需要这样做,所以如果能正确地完成这个任务,这样系统就可以处理它,那就太好了。

    我试过(但没成功):

    • 将三种不同的布局放在三个不同的文件夹中,称为“layout”、“layout-sw320dp”、“layout-sw400dp”
    • 将三个不同的布局放在三个不同的文件夹中,称为“layout”、“layout xxhdpi”、“layout xxxhdpi”

    非常感谢您提前回答!

    编辑: 布局实际上比这里描述的要复杂一些。实际布局如下:

    <LinearLayout>
       <RelativeLayout>
         <ImageView />
         <LinearLayout>
            <LinearLayout>
               <ImageView />
               <TextView />
            </LinearLayout>
            <LinearLayout>
               <ImageView />
               <TextView />
            </LinearLayout>
         </LinearLayout>
       </RelativeLayout>
     </LinearLayout>
    

    另外,第一个imageview中的图像是从Internet加载的,在加载之前我不知道它的大小。

    3 回复  |  直到 6 年前
        1
  •  1
  •   Zeon    6 年前

    我为你创建这个布局。您只需要创建一个XML文件并将项目放在此容器中。设置元素的重要性。如果没有足够的空间,重要性较低的项目将被删除。如果你需要补充或改变什么,请告诉我。在我的设备上它工作。

    例子

    <com.sup.dev.android.views.widgets.layouts.LayoutImportance
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/red_700"
            android:text="text_1"
            app:LayoutImportance_Layout_importance="5"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/blue_700"
            app:LayoutImportance_Layout_importance="1"
            android:text="teeeeeeeeext_2"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/green_700"
            app:LayoutImportance_Layout_importance="2"
            android:text="teeeeeeeeext_3_teeeeext"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/amber_700"
            app:LayoutImportance_Layout_importance="3"
            android:text="teeeeeeeeext_4_teeeeeeeeeeeeeeeeeeext"/>
    
    </com.sup.dev.android.views.widgets.layouts.LayoutImportance>
    

    layoutimportance.java

    public class LayoutImportance extends LinearLayout {
    
    private int lock;
    
    public LayoutImportance(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
        if (lock == 0) for (int i = 0; i < getChildCount(); i++) getChildAt(i).setVisibility(VISIBLE);
    
        int w = MeasureSpec.getSize(widthMeasureSpec);
    
        super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.UNSPECIFIED), heightMeasureSpec);
    
        if (getMeasuredWidth() > w) {
    
            ArrayList<View> children = new ArrayList<>();
            for (int i = 0; i < getChildCount(); i++) if (getChildAt(i).getVisibility() == VISIBLE) children.add(getChildAt(i));
            if(children.isEmpty())return;
    
            Collections.sort(children, (o1, o2) -> ((LayoutParams) o1.getLayoutParams()).importance - ((LayoutParams) o2.getLayoutParams()).importance);
            children.get(0).setVisibility(GONE);
    
            lock++;
            onMeasure(widthMeasureSpec, heightMeasureSpec);
            lock--;
        }
    
    }
    
    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }
    
    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(getContext(), null);
    }
    
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }
    
    @Override
    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p.width, p.height);
    }
    
    
    public static class LayoutParams extends LinearLayout.LayoutParams {
    
        public int importance;
    
        public LayoutParams(Context context, AttributeSet attrs) {
            super(context, attrs);
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LayoutImportance_Layout);
            importance = a.getInt(R.styleable.LayoutImportance_Layout_LayoutImportance_Layout_importance, 0);
            a.recycle();
        }
    
        public LayoutParams(int w, int h) {
            super(w, h);
        }
    
    }
    

    }

    属性.xml

    <declare-styleable name="LayoutImportance_Layout">
        <attr name="LayoutImportance_Layout_importance" format="integer"/>
    </declare-styleable>
    
        2
  •  0
  •   Ahsan bhatti    6 年前

    我想你需要得到每个页眉的布局宽度,并为每个页眉制作不同的布局。然后通过编程检查屏幕大小,在放大布局之前,你必须根据宽度设置页眉。

    使用此代码获取设备的宽度:

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;
    

    然后

    if(width==smallDevice){   //or whatever condition you want
    setHeaderLayoutFor("smallDevice");
    }
    
    else if(width=="mediumDevice"){
    setHeaderLayoutFor("mediumDevice");
    }
    else if(width=="largerDevice"){
    setHeaderLayoutFor("largeDevice");
    }
    

    并使setheaderlayoutor(string layouttype)设置/充气布局。

        3
  •  0
  •   Manoj Kumar    6 年前

    您可以使用“可伸缩dp”(sdp)。这个尺寸单位与屏幕尺寸成比例。

    要将sdp添加到项目中(使用android studio和gradle):

    添加 实现“com.intuit.sdp:sdp android:1.0.5” 到build.gradle dependencies块。

    dependencies {
      implementation 'com.intuit.sdp:sdp-android:1.0.5'
    }
    

    按如下所示设计XML:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:gravity="center">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <LinearLayout
                    android:id="@+id/give_us_a_review_landmine_main_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingBottom="@dimen/_27sdp"
                    android:paddingLeft="@dimen/_43sdp"
                    android:paddingRight="@dimen/_43sdp"
                    android:paddingTop="@dimen/_50sdp" >
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Intuit"
                            android:textColor="@android:color/black"
                            android:textSize="@dimen/_40sdp"/>
    
                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/_minus10sdp"
                                android:paddingBottom="@dimen/_15sdp"
                                android:orientation="horizontal" >
    
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:includeFontPadding="false"
                                android:text="♡"
                                android:textColor="#ED6C27"
                                android:textSize="@dimen/_70sdp"
                                android:textStyle="bold" />
    
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:includeFontPadding="false"
                                android:text="U"
                                android:textColor="@android:color/black"
                                android:textSize="@dimen/_70sdp" />
                            </LinearLayout>
    
                            <TextView
                                android:id="@+id/give_us_a_review_landmine_text_1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="center"
                                android:paddingBottom="@dimen/_12sdp"
                                android:text="Rate us so we can grow and help more people get their finances in check"
                                android:textColor="@android:color/black"
                                android:textSize="@dimen/_16sdp" />
    
                            <TextView
                                android:id="@+id/give_us_a_review_landmine_text_2"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center"
                                android:text="★★★★★"
                                android:textColor="#747474"
                                android:textSize="@dimen/_22sdp"
                                android:textStyle="bold" />
    
                            <Button
                                android:id="@+id/give_us_a_review_landmine_button"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:layout_marginTop="@dimen/_25sdp"
                                android:padding="@dimen/_8sdp"
                                android:text="Rate"
                                android:textSize="@dimen/_15sdp"
                                android:visibility="visible"
                                android:textColor="@android:color/white"
                                android:gravity="center"
                                android:minWidth="120dp"
                                android:includeFontPadding="false"
                                android:background="#0ac775"
                                android:singleLine="true" />
    
                </LinearLayout>
            </LinearLayout>
    
    </RelativeLayout>
    

    有关详细信息,请查看链接: https://github.com/intuit/sdp