Android 如何允许应用程序自动适应不同的屏幕宽度
我做了一些深入的研究,但没有找到任何可行的解决方案 我的问题是: 我的所有屏幕都包含标题布局。根据屏幕的宽度,我想显示三种不同的标题布局之一。旁注;我的应用程序只支持肖像模式 我的布局是: [图像][按钮][文本][按钮][文本]->用于大屏幕 [图像][按钮][文本][按钮]->用于中等屏幕 [图像][按钮][按钮]->用于小屏幕 它们应该由应用程序根据屏幕宽度自动选择。我想通过编程的方式来做这件事,因为我几乎需要在每一个活动中都这样做,所以如果这件事做得正确,系统就能处理它,那就太好了 我试过了,但没有成功: 将三个不同的布局放在三个不同的文件夹中,分别称为layout、layout-sw320dp、layout-sw400dp 将三种不同的布局放在三个不同的文件夹中,分别称为布局、布局xxhdpi、布局xxxhdpi 非常感谢您提前回答 编辑: 布局实际上看起来比这里描述的要复杂一些。实际布局如下所示:Android 如何允许应用程序自动适应不同的屏幕宽度,android,layout,android-screen-support,Android,Layout,Android Screen Support,我做了一些深入的研究,但没有找到任何可行的解决方案 我的问题是: 我的所有屏幕都包含标题布局。根据屏幕的宽度,我想显示三种不同的标题布局之一。旁注;我的应用程序只支持肖像模式 我的布局是: [图像][按钮][文本][按钮][文本]->用于大屏幕 [图像][按钮][文本][按钮]->用于中等屏幕 [图像][按钮][按钮]->用于小屏幕 它们应该由应用程序根据屏幕宽度自动选择。我想通过编程的方式来做这件事,因为我几乎需要在每一个活动中都这样做,所以如果这件事做得正确,系统就能处理它,那就太好了 我试
<LinearLayout>
<RelativeLayout>
<ImageView />
<LinearLayout>
<LinearLayout>
<ImageView />
<TextView />
</LinearLayout>
<LinearLayout>
<ImageView />
<TextView />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
此外,第一个ImageView中的图像是从internet加载的,在加载之前,我不知道它的大小。我认为您必须获得每个标题的布局宽度,并为每个标题制作不同的布局。然后通过编程检查屏幕大小,在扩大布局之前,您必须设置标题尊重你的宽度 使用以下代码获取设备的宽度:
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");
}
并使setHeaderLayoutForString layoutType设置/充气布局 我为您创建此布局。您只需要创建一个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);
}
}
}
attrs.xml
<declare-styleable name="LayoutImportance_Layout">
<attr name="LayoutImportance_Layout_importance" format="integer"/>
</declare-styleable>
您可以使用可扩展的dpsdp。此大小单位随屏幕大小缩放 要使用Android Studio和Gradle将sdp添加到项目中,请执行以下操作: 将实现“com.intuit.sdp:sdp android:1.0.5”添加到build.gradle依赖项块
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>
查看链接了解更多信息:我非常喜欢这个解决方案!但我有两个问题:1。我的标题中没有简单的元素列表,这只是为了简化问题。我实际上有多个布局和东西在那里,我需要隐藏一个或多个文本,如果需要的话。我将把这一点添加到我的初始问题中。2.我在标题中有一个从网络加载的图像,所以在我得到它之前我不知道图像的大小…好吧,我根据我的需要稍微调整了一下你的解决方案,现在它就像一个符咒一样工作。谢谢!