标签: android-percent-library

PercentRelativeLayout,如何以编程方式设置高度

我正在使用来自android percent支持包的PercentRelativeLayout.这就是我的布局.

<android.support.percent.PercentRelativeLayout
    android:id="@+id/view_parent"
    app:layout_heightPercent="68%" android:visibility="invisible"
    android:layout_width="match_parent"
    android:background="@color/mountain_meadow" android:layout_alignParentTop="true"
    android:layout_height="wrap_content">


    <View android:id="@+id/view_child" android:layout_width="match_parent" app:layout_heightPercent="30%"
        android:layout_alignParentBottom="true"
        />

</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我想以view_child编程方式更改高度.我怎么能用它PercentRelativeLayout.LayoutParams或任何其他方式做到这一点.

java android android-relativelayout android-percent-library

29
推荐指数
1
解决办法
7593
查看次数

替换linearLayout权重机制

背景:

  • 由于性能原因,Google建议避免使用嵌套加权linearLayouts.
  • 使用嵌套加权linearLayout很难读,写和维护.
  • 对于放置可用大小百分比的视图,仍然没有其他好的选择.只有解决方案才是权重并使用OpenGL.甚至没有类似WPF/Silverlight上显示的"viewBox"来自动缩放内容.

这就是为什么我决定创建我自己的布局,你要告诉每个孩子他们的重量(和周围的重量)与它的大小相比应该是什么.

看来我已经成功了,但出于某种原因,我认为有一些我无法追查的错误.

其中一个错误就是textView,即使我为它提供了大量空间,它也会将文本放在顶部而不是放在中心.另一方面,imageViews工作得很好.另一个错误是,如果我在自定义布局中使用布局(例如frameLayout),则其中的视图将不会显示(但布局本身将会显示).

请帮我弄清楚它为什么会发生.

如何使用:而不是线性布局的下一个用法(我故意使用长XML,以显示我的解决方案如何缩短事物):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical">

  <View android:layout_width="wrap_content" android:layout_height="0px"
    android:layout_weight="1" />

  <LinearLayout android:layout_width="match_parent"
    android:layout_height="0px" android:layout_weight="1"
    android:orientation="horizontal">

    <View android:layout_width="0px" android:layout_height="wrap_content"
      android:layout_weight="1" />

    <TextView android:layout_width="0px" android:layout_weight="1"
      android:layout_height="match_parent" android:text="@string/hello_world"
      android:background="#ffff0000" android:gravity="center"
      android:textSize="20dp" android:textColor="#ff000000" />

    <View android:layout_width="0px" android:layout_height="wrap_content"
      android:layout_weight="1" />

  </LinearLayout>
  <View android:layout_width="wrap_content" android:layout_height="0px"
    android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我所做的只是(x是将视图本身放在权重列表中的位置):

<com.example.weightedlayouttest.WeightedLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res/com.example.weightedlayouttest"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" tools:context=".MainActivity">

  <TextView android:layout_width="0px" android:layout_height="0px"
    app:horizontalWeights="1,1x,1" app:verticalWeights="1,1x,1"
    android:text="@string/hello_world" android:background="#ffff0000"
    android:gravity="center" android:textSize="20dp" android:textColor="#ff000000" />

</com.example.weightedlayouttest.WeightedLayout>
Run Code Online (Sandbox Code Playgroud)

我的特殊布局代码是:

public class WeightedLayout extends ViewGroup
  {
  @Override …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-linearlayout android-layout-weight android-percent-library

18
推荐指数
2
解决办法
2191
查看次数

缺少PercentRelativeLayout百分比值的XML资源类型?

似乎新的百分比支持库已发布,但不允许在维度xml文件中引用百分比值.

也就是说,而不是:

 <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.PercentFrameLayout/>
Run Code Online (Sandbox Code Playgroud)

能够编写类似的代码:

 <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="@percent/myWidthPercent"
     app:layout_heightPercent="@percent/my/HeightPercent"
     app:layout_marginTopPercent="@percent/myMarginTophPercent"
     app:layout_marginLeftPercent="@percent/myMarginLeftPercent"/>
Run Code Online (Sandbox Code Playgroud)

其中myWidthPercent在资源文件中定义.

我错了(我是否错过了另一个名字)或者是我们可以发送给谷歌的功能请求?

android android-resources android-percent-library

15
推荐指数
1
解决办法
1190
查看次数

将基线与TextView进行对齐,TextView包含在TextInputLayout中

我想将TextView"Deficient"的基线与EditText"10000"的基线对齐.他们都在里面<android.support.percent.PercentRelativeLayout>

在此输入图像描述

我尝试了以下,但无法让它工作.

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout_soil_nitrogen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:layout_widthPercent="63%">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter N of soil"
            tools:text="10000" />

    </android.support.design.widget.TextInputLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/textInputLayout_soil_nitrogen"
        android:layout_toRightOf="@id/textInputLayout_soil_nitrogen"
        app:layout_widthPercent="37%"
        tools:text="Deficient" />

</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)

给TextView类型提供20dp的顶部填充可以解决问题,但是对齐它们的基线会很不错.在这种情况下,这甚至可能吗?

我删除了textAppearance和id属性,顺便说一句.

android android-layout android-textinputlayout android-percent-library

10
推荐指数
2
解决办法
3124
查看次数

PercentFrameLayout:您必须提供layout_width属性

我尝试使用PercentFrameLayout但我收到此错误消息:

Binary XML file line #: You must supply a layout_width attribute.
Run Code Online (Sandbox Code Playgroud)

这是我使用的xml:

<android.support.percent.PercentFrameLayout
    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_heightPercent="50%"
        app:layout_marginLeftPercent="25%"
        app:layout_marginTopPercent="25%"
        app:layout_widthPercent="50%"/>
</android.support.percent.PercentFrameLayout>
Run Code Online (Sandbox Code Playgroud)

这是一个错误还是我的错?

android android-layout android-percent-library android-percent-layout

5
推荐指数
1
解决办法
1687
查看次数

在Android Studio中安装com:android:support:percent:22.2.1库失败

安装com:android:support:percent:22.2.1库在Android Studio中失败,尽管我在SDK Manager中安装了Android支持库和Android支持存储库.

有谁知道如何解决这一问题?

这是我的gradle文件:

dependencies {
    compile 'de.greenrobot:eventbus:2.4.0'
    compile 'com.android.support:support-v4:22.0.0'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.kbeanie:image-chooser-library:1.4.3@aar'
    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
    compile(name:'Chart-2015.1.423-dev-release', ext:'aar')
    compile(name:'Common-2015.1.423-dev-release', ext:'aar')
    compile(name:'Data-2015.1.423-dev-release', ext:'aar')
    compile(name:'Feedback-2015.1.423-dev-release', ext:'aar')
    compile(name:'Input-2015.1.423-dev-release', ext:'aar')
    compile(name:'List-2015.1.423-dev-release', ext:'aar')
    compile(name:'Primitives-2015.1.423-dev-release', ext:'aar')
    compile 'net.danlew:android.joda:2.7.1'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.3.2@aar') {
        transitive = true;
    }
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.android.support:multidex:1.0.0'
 }
Run Code Online (Sandbox Code Playgroud)

android android-support-library android-percent-library

5
推荐指数
1
解决办法
6779
查看次数

开关元素未在分配的空间内水平居中对齐

创建 后PercentRelativeLayout,我注意到SwitchCompat尽管设置了属性,但控件并未水平居中。为了解决这个问题可以做什么?我尝试使用android:layout_centerHorizontal="true",但这似乎不起作用。

<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="wrap_content"
    android:layout_weight="10"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp">
    <ImageView
        android:id="@+id/imageView1"
        app:layout_widthPercent="40%"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_image1" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_map_emiratesairline_emiratesgreenwichpeninsula"
        app:layout_widthPercent="20%"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:theme="@style/Theme.AppCompat.Light"
        android:background="@android:color/transparent"
        android:layout_toEndOf="@id/imageView1"/>

    <ImageView
        android:id="@+id/imageView2"
        app:layout_widthPercent="40%"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_image2"
        android:layout_toEndOf="@id/switch_tgl"/>
</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)

xml android android-layout android-relativelayout android-percent-library

2
推荐指数
1
解决办法
1571
查看次数

Android - 将父线性布局的宽度设置为屏幕的百分比

美好的一天.道歉令人困惑的标题.

我正在构建一个Android应用程序,我有一个对话框片段.对话框片段具有设置的宽度.但是,问题是当应用程序在具有不同屏幕大小的设备上运行时,对话框片段未正确居中.

最初,我所拥有的是:

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

  <-- code goes here -->

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,我有一个具有定义宽度的relativeLayout.因为我知道你不能layout_weight在相对布局中使用,所以我做的是将线性布局中的父相对布局包裹起来:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.8"
    android:weightSum="1">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    </RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用,因为当我在具有较小屏幕尺寸的设备上运行应用程序时,会切断对话框片段.

如何将对话框片段的宽度设置为屏幕大小的百分比?这是可能的还是我不得不求助于以编程方式设置它?

非常感谢任何帮助,谢谢.

android android-layout android-dialogfragment android-percent-library

0
推荐指数
1
解决办法
7960
查看次数