我有两个微调器和一个方形图像视图。我已经设置了固定布局的权重,以便在每个屏幕中所有屏幕的比例都相同,如小,普通,大和xlarge。我放弃了小。图片如下: enter image description here
我使用了运行时布局权重,如下所示:
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>
我会很高兴这里有任何可能的解决方案。因为我可以通过创建多个布局和最小宽度来解决这个问题。
答案 0 :(得分:0)
如果您在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>
这里我只是给出提示你的布局结构应该是什么样子的提示。如果你遵循这个,那么你不需要根据设备屏幕以编程方式更改权重,因为它对所有人都是相同的。