【问题标题】:Changing sizes between layouts在布局之间更改大小
【发布时间】:2017-07-08 20:34:11
【问题描述】:

我有一个LinearLayout。在里面,我有几个Fragments。我想在另一个Activity 中使用我的顶部Fragment,但保持完全相同的大小。我的问题是,下一个ActivityRelativeLayout,而android:weightSum 不可用。如何保持我的尺寸从 LinearLayoutRelativeLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/appmenu"
    android:weightSum="6.5">
    <fragment
        android:id="@+id/titlebar"
        class="com.abc.appname.titlebar"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <fragment
        android:id="@+id/mainmenubuttons"
        class="com.abc.appname.mainmenubuttons"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="4.5"/>
    <fragment
        android:id="@+id/bottombar"
        class="com.abc.appname.bottombar"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

解决方案: 我使用了如下建议的 RelativeLayout Percentages。我想指出,我必须添加一些东西才能使其正常工作,包括:

在 Gradle 中:

 compile 'com.android.support:percent:23.3.0'

在 XML 中:

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"

希望这对下一个人有帮助:)

【问题讨论】:

  • RelativeLayoutAcitivity的邮政编码
  • 现在 Activity 只有一个没有任何内容的 RelativeLayout,因为我无法获得片段的特定大小。它将与 LinearLayout 几乎相同。将 LL 切换为 RL 不会有任何问题,但我试图保持屏幕尺寸之间的比例。

标签: java android xml fragment


【解决方案1】:

使用ParcentRelativeLayouthttps://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

简单的实现

  <android.support.percent.PercentRelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/appmenu"
    >
    <fragment
        android:id="@+id/titlebar"
        class="com.abc.appname.titlebar"
        android:layout_width="fill_parent"

         app:layout_heightPercent="14.5%"/>
    <fragment
        android:id="@+id/mainmenubuttons"
        class="com.abc.appname.mainmenubuttons"
        android:layout_width="fill_parent"

         app:layout_heightPercent="69%"/>
    <fragment
        android:id="@+id/bottombar"
        class="com.abc.appname.bottombar"
        android:layout_width="fill_parent"

        app:layout_heightPercent="14.5%"/>

</LinearLayout>

【讨论】:

  • 太棒了!谢谢!我不知道这存在。正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
相关资源
最近更新 更多