我的应用中有一个4项启动画面,如下所示:
对我来说重要的是: - 所有项目都具有相同的宽度(不考虑实际文本中有多少文本) - 在所有设备上看起来都一样(小屏幕,mdpi,大屏幕等)
我只是想知道这个问题是否有一个简单的解决方案?
我尝试使用3个LinearLayouts,但那真的很尴尬.. (1表示布局根[垂直],两个表示每个包含2个按钮[水平])。 使这种布局为多个屏幕做好准备将需要大量固定宽度和固定边距的黑客攻击。就像“按钮边距= xlarge上的30dp,20上大,15正常,......”。
我的layout-xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:id="@+id/main_root"
android:orientation="vertical"
android:gravity="center" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="@+id/btn_learn"
android:text="@string/mainBtn_learn"
style="@style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
<Button
android:id="@+id/btn_quiz"
android:text="@string/mainBtn_quiz"
style="@style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="@+id/btn_search"
android:text="@string/mainBtn_search"
style="@style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
<Button
android:id="@+id/btn_more"
android:text="@string/mainBtn_more"
style="@style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
</LinearLayout>
是否有一个“自动缩放”这些按钮的视图或者还有其他更简单的解决方案?
所以,特别的,你需要类似的东西 按钮:
android:layout_width="15%" // 15% of screen width / height depending on the orientation
android:layout_marginBottom="10%" // see above
答案 0 :(得分:1)
你是否已经拥有xml,可以在一个屏幕尺寸上工作?如果发布了迄今为止的内容。
我建议你的root用户使用RelativeLayout。您可以使用alignCenter属性将您的孩子漂浮到中间。然后你只需要硬编码内边距(你想要按钮多远),而不是从你自己到墙上的边距。
您还可以通过制作自己的按钮9补丁图像来避免硬编码内边距。您只需在图像中添加透明像素边框即可表示边距。您可能仍希望为您希望支持的每个密度提供图像。
答案 1 :(得分:1)
我对Android开发很新,但我可以告诉你在类似情况下对我有用的东西。我将我的布局定义如下:
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/outputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:editable="false" />
<Spinner
android:id="@+id/outputSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:prompt="@string/OutputBaseOptionsPrompt" />
</LinearLayout>
我有两个项目的水平布局。 LinearLayout的宽度为“match_parent”,因此它与屏幕一样宽。布局中的两个项目都具有以下内容:
android:layout_width="0dp"
android:layout_weight="1"
由于两个项目的layout_weight都为1,因此它们将以相同的宽度绘制。在这种情况下,每个项目占用可用空间的一半。如果您将其中一个项目的重量更改为“2”,那么它将是重量为“1”的项目的两倍宽。
答案 2 :(得分:0)
解决方案是你不要在任何地方使用硬编码值 将三个具有相同名称的图像放在hdpi mdpi和ldpi文件夹中的drawables中
运行代码