android - 不同屏幕的布局权重问题
问题描述
我有两个微调器和一个方形 imageview 。我已经设置了固定布局的权重,以便在每个屏幕中,所有屏幕的比例都相同,例如小、正常、大和 xlarge。我已经丢弃了小的。图片如下: 在此处输入图片描述
我已经像这样使用了运行时布局权重:
private void initfixview()
{
LinearLayout.LayoutParams lnrspnrviewlayoutparams= (LinearLayout.LayoutParams)lnrspnrview.getLayoutParams();
LinearLayout.LayoutParams lnrimageviewlayoutparams= (LinearLayout.LayoutParams)lnrimageview.getLayoutParams();
LinearLayout.LayoutParams lnrdummyspacelayoutparams= (LinearLayout.LayoutParams)lnrdummyspace.getLayoutParams();
LinearLayout.LayoutParams lnrimgarrowupdownlayoutparams= (LinearLayout.LayoutParams)lnrimgarrowupdown.getLayoutParams();
LinearLayout.LayoutParams lnrcountrystatelayoutparams= (LinearLayout.LayoutParams)lnrcountrystate.getLayoutParams();
LinearLayout.LayoutParams lnrzipcodelayoutparams= (LinearLayout.LayoutParams)lnrzipcode.getLayoutParams();
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE)
{
lnrspnrviewlayoutparams.weight=80;
lnrspnrviewlayoutparams.width=80;
lnrimageviewlayoutparams.weight=20;
lnrimageviewlayoutparams.width=20;
lnrspnrview.setLayoutParams(lnrspnrviewlayoutparams);
lnrimageview.setLayoutParams(lnrimageviewlayoutparams);
lnrdummyspacelayoutparams.weight=80;
lnrdummyspacelayoutparams.width=80;
lnrimgarrowupdownlayoutparams.weight=20;
lnrimgarrowupdownlayoutparams.width=20;
lnrdummyspace.setLayoutParams(lnrspnrviewlayoutparams);
lnrimgarrowupdown.setLayoutParams(lnrimageviewlayoutparams);
lnrcountrystatelayoutparams.weight=80;
lnrcountrystatelayoutparams.width=80;
lnrzipcodelayoutparams.weight=20;
lnrzipcodelayoutparams.width=20;
lnrcountrystate.setLayoutParams(lnrspnrviewlayoutparams);
lnrzipcode.setLayoutParams(lnrimageviewlayoutparams);
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL)
{
lnrspnrviewlayoutparams.weight=70;
lnrspnrviewlayoutparams.width=70;
lnrimageviewlayoutparams.weight=30;
lnrimageviewlayoutparams.width=30;
lnrspnrview.setLayoutParams(lnrspnrviewlayoutparams);
lnrimageview.setLayoutParams(lnrimageviewlayoutparams);
lnrdummyspacelayoutparams.weight=70;
lnrdummyspacelayoutparams.width=70;
lnrimgarrowupdownlayoutparams.weight=30;
lnrimgarrowupdownlayoutparams.width=30;
lnrdummyspace.setLayoutParams(lnrspnrviewlayoutparams);
lnrimgarrowupdown.setLayoutParams(lnrimageviewlayoutparams);
lnrcountrystatelayoutparams.weight=70;
lnrcountrystatelayoutparams.width=70;
lnrzipcodelayoutparams.weight=30;
lnrzipcodelayoutparams.width=30;
lnrcountrystate.setLayoutParams(lnrspnrviewlayoutparams);
lnrzipcode.setLayoutParams(lnrimageviewlayoutparams);
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE)
{
lnrspnrviewlayoutparams.weight=85;
lnrspnrviewlayoutparams.width=85;
lnrimageviewlayoutparams.weight=15;
lnrimageviewlayoutparams.width=15;
lnrspnrview.setLayoutParams(lnrspnrviewlayoutparams);
lnrimageview.setLayoutParams(lnrimageviewlayoutparams);
lnrdummyspacelayoutparams.weight=85;
lnrdummyspacelayoutparams.width=85;
lnrimgarrowupdownlayoutparams.weight=15;
lnrimgarrowupdownlayoutparams.width=15;
lnrdummyspace.setLayoutParams(lnrspnrviewlayoutparams);
lnrimgarrowupdown.setLayoutParams(lnrimageviewlayoutparams);
lnrcountrystatelayoutparams.weight=85;
lnrcountrystatelayoutparams.width=85;
lnrzipcodelayoutparams.weight=15;
lnrzipcodelayoutparams.width=15;
lnrcountrystate.setLayoutParams(lnrspnrviewlayoutparams);
lnrzipcode.setLayoutParams(lnrimageviewlayoutparams);
}
else
{
lnrspnrviewlayoutparams.weight=70;
lnrspnrviewlayoutparams.width=70;
lnrimageviewlayoutparams.weight=30;
lnrimageviewlayoutparams.width=30;
lnrspnrview.setLayoutParams(lnrspnrviewlayoutparams);
lnrimageview.setLayoutParams(lnrimageviewlayoutparams);
lnrdummyspacelayoutparams.weight=70;
lnrdummyspacelayoutparams.width=70;
lnrimgarrowupdownlayoutparams.weight=30;
lnrimgarrowupdownlayoutparams.width=30;
lnrdummyspace.setLayoutParams(lnrspnrviewlayoutparams);
lnrimgarrowupdown.setLayoutParams(lnrimageviewlayoutparams);
lnrcountrystatelayoutparams.weight=70;
lnrcountrystatelayoutparams.width=70;
lnrzipcodelayoutparams.weight=30;
lnrzipcodelayoutparams.width=30;
lnrcountrystate.setLayoutParams(lnrspnrviewlayoutparams);
lnrzipcode.setLayoutParams(lnrimageviewlayoutparams);
}
edtcompanyname.setHint(Html.fromHtml(getString(R.string.namecompany)));
edtusertelephone.setHint(Html.fromHtml(getString(R.string.telephone)));
}
现在在设备中它工作正常,但在某些设备中,该比率不像普通的小尺寸设备那样保持不变。
我已经尝试了所有可能的解决方案,例如最小宽度、密度因子以及为相同的创建多个布局:
发布的图像如下:
我的布局代码如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginEnd="@dimen/pleaseselectonemarginstart"
android:layout_marginLeft="@dimen/pleaseselectonemarginstart"
android:layout_marginRight="@dimen/pleaseselectonemarginstart"
android:layout_marginStart="@dimen/pleaseselectonemarginstart"
android:layout_marginTop="10dp"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:id="@+id/lnrspnrview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="70"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@drawable/shadow_white_normal"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal">
<Spinner
android:id="@+id/spnrcountry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/sfdmedium"
android:gravity="center|left"
android:hint="@string/telephone"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:textColor="@color/edtcolor"
android:textColorHint="@color/hintcolor"
android:textSize="@dimen/textsize" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="@drawable/shadow_white_normal"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal">
<Spinner
android:id="@+id/spnrbusinesscategory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/sfdmedium"
android:gravity="center|left"
android:hint="@string/telephone"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:textColor="@color/edtcolor"
android:textColorHint="@color/hintcolor"
android:textSize="@dimen/textsize" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/lnrimageview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_weight="30"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<custom.SquareImageView
android:id="@+id/edtuploadimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="center"
android:scaleType="fitXY"
android:src="@drawable/shadow_logo" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:fontFamily="@font/sfdmedium"
android:gravity="center"
android:text="@string/uploadlogo"
android:textColor="@color/hintcolor"
android:textSize="@dimen/lessertextsize" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
我会很高兴在这里找到任何可能的解决方案。因为我通过创建多个布局和最小宽度等来解决这个问题。
解决方案
如果您在 xml 中设置权重,则无需在运行时根据设备屏幕进行更改,它将为所有屏幕保留相同的比例。我对你的布局做了一些更正:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/lnrspnrview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Spinner
android:id="@+id/spnrcountry"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Spinner
android:id="@+id/spnrbusinesscategory"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4">
<custom.SquareImageView
android:id="@+id/edtuploadimage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center" />
</RelativeLayout>
在这里,我只是提示您的布局结构应该是什么样子。如果您遵循此操作,则无需根据设备屏幕以编程方式更改权重,因为它对所有人都是相同的。
推荐阅读
- wordpress - 在自定义列之后排序时,Wordpress 媒体库缺少具有 NULL 值的条目
- c# - C# 性能改进中的 SIMD 字符串到无符号整数解析
- databricks - UI 确实在 MLflow 中显示数据
- python - Python(manimgl)在当前目录中找不到模块
- docker-compose - 使用 docker-compose 移动我的 hasura 云架构、关系、表等并放入我的离线 docker 文件中
- python - args 在 scipy 优化中究竟做了什么?
- javascript - Vue商店调度错误响应未传递给UI
- python - 大熊猫相同列中的行总和
- reactjs - 来自 babel-loader@7.1.2 的 peer webpack@"2 || 3"
- javascript - 如何将 PayPal 按钮集成到 Extjs 环境中?