【问题标题】:How to set different percentages for Mobile & Tablets如何为手机和平板电脑设置不同的百分比
【发布时间】:2016-10-04 08:16:51
【问题描述】:

我目前正在使用 android.support.percent API

我的示例代码

<android.support.percent.PercentRelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>
 <TextView
     android:text="Completed"
     app:layout_widthPercent="40%"
     app:layout_heightPercent="40%"
     app:layout_marginTopPercent="15%"
     app:layout_marginLeftPercent="15%"/>
</android.support.percent.PercentRelativeLayout/>

我的问题:

百分比相对布局在平板电脑中正确显示...但有些视图在手机中显示非常小。

所以我只想增加手机的百分比。而不是平板电脑。

我的问题:

如何使用百分比相对布局为手机和平板电脑设置不同的百分比。(如不同的尺寸)

app:layout_heightPercent="40%" // 适合平板电脑 但我想提高到 50% 的电话..

【问题讨论】:

标签: android android-layout android-percent-layout


【解决方案1】:

您可以使用最小宽度尺寸

<android.support.percent.PercentRelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>
 <TextView
     android:text="Completed"
     app:layout_widthPercent="@fraction/width"
     app:layout_heightPercent="@fraction/height"
     app:layout_marginTopPercent="@fraction/margin_top"
     app:layout_marginLeftPercent="@fraction/margin_left"/>
</android.support.percent.PercentRelativeLayout/>

尺寸:

dimens.xml

<fraction name="width">40%</fraction>
    <fraction name="height">40%</fraction>
    <fraction name="margin_top">15%</fraction>
    <fraction name="margin_left">15%</fraction>

和dimens.xml (sw 600dp)

<fraction name="width">40%</fraction>
    <fraction name="height">50%</fraction>
    <fraction name="margin_top">15%</fraction>
    <fraction name="margin_left">15%</fraction>

你可以看到更多设计屏幕的细节here

【讨论】:

  • 抱歉不工作..它显示错误 - 我编译时(不允许分数)
  • @RanjithKumar 抱歉,我搞错了。我确实编辑了它。现在应该可以工作了。
【解决方案2】:

你应该看看我的回答就在这里https://stackoverflow.com/a/39571409/6644403

平板电脑尺寸很大,因此请使用带有“大”或“X 大”限定符的百分比的当前布局,并为移动设备创建另一个没有限定符或“普通”限定符的布局,您可以在其中更改视图大小。

【讨论】:

  • 我知道不同维度的文件夹结构..我在问如何存储百分比值
  • 如何使用百分比相对布局为手机和平板电脑设置不同的百分比。(如不同的尺寸) app:layout_heightPercent="40%" // 适合平板电脑但我想将手机的百分比增加到 50% ..您的问题不是关于存储百分比值还是我误解了?
【解决方案3】:

对于解决方法,您可以为不同的屏幕使用不同的布局文件夹。对于手机 layout 文件夹和平板电脑 layout-large

screen.xml 用于 layout 文件夹

<android.support.percent.PercentRelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>
 <TextView
     android:text="Completed"
     app:layout_widthPercent="40%"
     app:layout_heightPercent="40%"
     app:layout_marginTopPercent="15%"
     app:layout_marginLeftPercent="15%"/>
</android.support.percent.PercentRelativeLayout/>

screen.xml 用于 layout-large 文件夹

<android.support.percent.PercentRelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>
 <TextView
     android:text="Completed"
     app:layout_widthPercent="80%"
     app:layout_heightPercent="80%"
     app:layout_marginTopPercent="30%"
     app:layout_marginLeftPercent="30%"/>
</android.support.percent.PercentRelativeLayout/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2014-03-27
    • 2014-07-27
    • 2017-03-16
    相关资源
    最近更新 更多