我正在使用来自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或任何其他方式做到这一点.
背景:
这就是为什么我决定创建我自己的布局,你要告诉每个孩子他们的重量(和周围的重量)与它的大小相比应该是什么.
看来我已经成功了,但出于某种原因,我认为有一些我无法追查的错误.
其中一个错误就是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
似乎新的百分比支持库已发布,但不允许在维度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在资源文件中定义.
我错了(我是否错过了另一个名字)或者是我们可以发送给谷歌的功能请求?
我想将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
我尝试使用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
安装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) 创建 后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
美好的一天.道歉令人困惑的标题.
我正在构建一个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