【问题标题】:set width and height as percentage in relative layout在相对布局中将宽度和高度设置为百分比
【发布时间】:2016-10-26 19:38:14
【问题描述】:

我想要这个:一个被 2 个箭头覆盖的 ViewPager

我通过这段代码得到了我想要的东西。但是,我收到了这些 lint 警告:

  • 这个布局没用(对于虚拟布局)
  • 嵌套权重不利于性能。

我想知道是否有更好的方法来获得我想要的这个 UI。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/dialog_instruction_pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>

<!-- this LinearLayout  overlays the ViewPager -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <!-- This is just the container for the left and right arrows , it is allocated with only 20% of screen height-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".2"
        android:orientation="horizontal" >

        <!-- I want to align this to the left and set width only 5% of the parent -->
        <ImageButton
            android:id="@+id/instruction_dialog_left_arrow"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".05"
            android:background="?android:attr/selectableItemBackground"
            android:scaleType="fitCenter"
            android:src="@drawable/arrow_left_grey" />

        <!-- This is the dummy layout, standing in the middle of 2 arrows so they can be align to the left and right of the parent -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".8" >
        </LinearLayout>

        <!-- I want to align this to the right and set width only 5% of the parent-->
        <ImageButton
            android:id="@+id/instruction_dialog_right_arrow"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".05"
            android:background="?android:attr/selectableItemBackground"
            android:scaleType="fitCenter"
            android:src="@drawable/arrow_right_grey" />
    </LinearLayout>
</LinearLayout>

P.S:是否有任何布局允许将子级左、右、下对齐(如 RelativeLayout)并允许将宽度、高度设置为百分比(如 LinearLayout 中的“权重”属性)?

谢谢!

【问题讨论】:

    标签: java android android-linearlayout android-layout-weight


    【解决方案1】:

    试试这个

    为避免此布局无用(对于虚拟布局),您应该android:background="@android:color/transparent" 并在该线性布局中添加虚拟视图,因为布局不应为空。。 p>

    为避免嵌套权重不利于性能,添加一个子线性布局。 并在代码中阅读我的 cmets

    并为imageview添加了父布局以使图像垂直居中

    试试这个代码

        <?xml version="1.0" encoding="utf-8"?>
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/dialog_instruction_pager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></android.support.v4.view.ViewPager>
    
        <!-- this LinearLayout  overlays the ViewPager -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
    
            <!-- This is just the container for the left and right arrows , it is allocated with only 20% of screen height-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".2"
                android:orientation="horizontal">
                <!--child layout to avoid nested weight with `waighSum` property-->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:weightSum="100">
    
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="5"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">
                        <!-- I want to align this to the left and set width only 5% of the parent -->
                        <ImageButton
                            android:id="@+id/instruction_dialog_left_arrow"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="?android:attr/selectableItemBackground"
                            android:scaleType="fitCenter"
                            android:src="@drawable/icon_clock" />
                    </LinearLayout>
                    <!-- This is the dummy layout, standing in the middle of 2 arrows so they can be align to the left and right of the parent -->
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="90"
                        android:background="@android:color/transparent">// this line also makes layout usefull
                        <!-- dummy view becouse layout should not be empty -->
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="match_parent" />
                    </LinearLayout>
    
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="5"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">
                        <!-- I want to align this to the right and set width only 5% of the parent-->
                        <ImageButton
                            android:id="@+id/instruction_dialog_right_arrow"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="?android:attr/selectableItemBackground"
                            android:scaleType="fitCenter"
                            android:src="@drawable/icon_clock" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </FrameLayout>
    

    希望对你有用

    【讨论】:

    • 感谢您的回答,它确实删除了 Lint 的所有警告。但是,在我看来,这个 xml 有许多嵌套布局。它会降低性能吗?
    • 不,它不会影响性能..当您在一个布局 xml 文件中添加具有 10 个连续嵌套布局和超过 80 个视图的视图时,只有效果..
    • 谢谢你的解释,我从你那里学到了一些有趣的东西。
    【解决方案2】:

    Google 推出了名为 android.support.percent

    的新 API

    1)PercentRelativeLayout

    2)PercentFrameLayout

    添加编译依赖如

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

    您可以以百分比指定维度,从而获得RelativeLayout 和百分比的好处

     <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
             app:layout_widthPercent="40%"
             app:layout_heightPercent="40%"
             app:layout_marginTopPercent="15%"
             app:layout_marginLeftPercent="15%"/>
     </android.support.percent.PercentRelativeLayout/>
    

    【讨论】:

      【解决方案3】:
      Try the below code and see whether it works, 
      This code will get the layout what you have attached
      
            <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" >
      
        <android.support.v4.view.ViewPager
          android:id="@+id/dialog_instruction_pager"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@color/theme_blue" >
      </android.support.v4.view.ViewPager>
      
      <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_centerInParent="true"
          android:background="@drawable/ic_launcher" />
      
        <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_centerInParent="true"
          android:background="@drawable/ic_launcher" />
      
       </RelativeLayout>
      

      【讨论】:

      • 感谢您的回答,您建议的代码似乎没有设置ImageView的宽度和高度,而只是“wrap_content”。我想以百分比设置图像的宽度和高度,并且与图像的尺寸无关。
      • 但是为什么要设置宽度和高度的百分比呢?默认情况下,wrapcontent 将采用图像大小。根据缩放比例将图像放置在所有对应的drawable中。这是android建议的标准
      • 非常感谢您耐心地向我解释 :) ,非常感谢。我正在做一个演示项目,我从互联网上下载并使用各种尺寸的图像。有些图像如果我将它们与包装内容一起使用,它在屏幕上会太大。我不知道您所说的“根据缩放比例将图像放置在所有相应的可绘制对象中”是什么意思,您能更具体一点或给我发个链接吗?非常感谢!
      • developer.android.com/guide/practices/screens_support.html 请阅读上述关于各种屏幕的 UI 文档。如果您使用标准图标,请从此处developer.android.com/design/style/iconography.html 下载。如果您自己创建,请遵循上述链接中提到的缩放比例。您有不同的文件夹,如 drawable-hdpi、xxhdpi 以适应应用程序中的不同屏幕和密度。在 android 中,您不能指定百分比,而是需要提供 dp,即每英寸的密度
      • 我在网上搜索了你的建议。你在说这个吗?缩放将用作图标的图片,然后创建 4 个版本。即 mdpi、hdpi、xhdip 和 xxhdpi 。使用Android Asset Studio创建版本,然后放到项目中对应的drawables文件夹中,Anroid会自动选择吗?
      【解决方案4】:

      尝试使用RelativeLayout,然后将左图设置为alignParentLeft,将右图设置为alignParentRight

      【讨论】:

      • 谢谢,但是设置图片的宽度和高度没有帮助。
      猜你喜欢
      • 2015-07-06
      • 1970-01-01
      • 2013-08-20
      • 2010-10-17
      • 2012-04-05
      • 2013-03-13
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多