【问题标题】:Android: Setting width percentage of view in RelativeLayoutAndroid:在RelativeLayout中设置视图的宽度百分比
【发布时间】:2011-10-05 20:05:43
【问题描述】:

我正在使用RelativeLayout,并希望将下方的橙色条居中并填充90% 的宽度。我知道使用百分比来使用 LinearLayout 可以做到这一点,但是如何使用 RelativeLayout 来做到这一点?

我是否必须以编程方式计算屏幕的宽度(以 dp 为单位),并将其中的 90% 设置为橙色视图的宽度?

简而言之,我想要这样的东西,橙色条占据屏幕的 90%,使用 RelativeLayout 居中居中,没有硬编码 x 和 y 坐标,因此它适用于所有屏幕密度。 (边缘的黑条是手机的边缘)

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    只是一个猜测,但值得一试:) 请重新设计您的布局结构,如下所示:

    <LinearLayout[with 90% width]>
        <RelativeLayout
               android:layout_width="fill_parent" 
               ...
             <!--this will fill up its parent i.e above LinearLayout which is 90% of the screen width -->
         >
               <!--child views goes here.. -->
         </RelativeLayout>
    </LinearLayout>
    

    【讨论】:

    • 其实,这就是我要找的。我是新手!
    【解决方案2】:

    使用新的百分比支持库

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

    见下例

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

    *更多信息*Tutorial1Tutorial2Tutorial3

    【讨论】:

      猜你喜欢
      • 2016-12-16
      • 2013-03-12
      • 1970-01-01
      • 2012-04-05
      • 2012-04-22
      • 1970-01-01
      • 2013-05-25
      • 2019-10-01
      • 2013-03-13
      相关资源
      最近更新 更多