我创建了一些自定义元素,我想以编程方式将它们放置在右上角(n顶部边缘的m像素和右边缘的像素).因此我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
Run Code Online (Sandbox Code Playgroud)
如何获得screenWidth并screenHeight在主要活动?
通过Android Studio 2.2的预览1,Google在其支持库中发布了一个新的布局:ConstraintLayout.使用ConstraintLayout,在Android Studio中使用设计工具更容易,但我没有找到一种方法来使用相对大小(像LinearLayout中的百分比或'权重').有没有办法根据百分比定义约束?例如,使视图占据屏幕的40%,在视图之间创建20%的边距,将视图的宽度设置为另一个视图宽度的50%?
我有一个线性布局的按钮.我希望按钮占据其父级宽度的一半.有没有办法在布局xml中执行此操作.
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:padding="6dp">
<Button
android:id="@+id/buttonCollect"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/hello"
android:paddingLeft="8dp"
android:paddingRight="8dp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的问题非常类似于将声明宽度分配给可用屏幕宽度的一半,除了我只有一个按钮.
我多次尝试绘制2个相对布局水平对齐并分为半屏.

我用油漆设计图像来更好地解释我的意思.
有什么建议吗?
我很难让我的imageview出现在屏幕的一半,有人可以提供帮助.这是我的xml文件.它应该占用屏幕尺寸的一半,并且带有textview的线性布局应该位于imageview的页脚.
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fond" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true" >
<WebView
android:id="@+id/webcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="151dp" />
</RelativeLayout>
<ImageView
android:id="@+id/image_actualitedetails"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/six_yamaha" />
<LinearLayout
android:id="@+id/linearvideo"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_alignBottom="@+id/image_actualitedetails"
android:layout_alignParentLeft="true"
android:background="#88FFFFFF"
android:gravity="center"
android:paddingLeft="@dimen/fourdp"
android:paddingRight="@dimen/fourdp" >
<TextView
android:id="@+id/text_actualites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/tabDark"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/header" >
<Button
android:id="@+id/boutton_retour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:background="@drawable/back" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我遇到了这个链接:
如何在相对布局中做同样的事情?这是代码:在相对布局中尝试linearlayout.但这不起作用.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:text="UID"
android:textColor="#ffe5f1f1" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffe5f1f1"
android:hint="Enter UID"
android:inputType="number"
android:shadowColor="5"
android:textColor="#ff009999"
android:textStyle="bold"
android:typeface="monospace" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_marginTop="18dp"
android:text="Button" />
<Button
android:id="@+id/button8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button7"
android:layout_weight="1"
android:layout_marginTop="16dp"
android:text="Button" />
<Button
android:id="@+id/button9"
android:layout_width="fill_parent" …Run Code Online (Sandbox Code Playgroud) 我有一些XML.在这个XML中,相对布局中有一个按钮.相对布局占据整个屏幕.我希望按钮有1/3的屏幕宽度和高度.我怎么能这样做?
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context="com.vroy.trapper.MainMenuActivity"
android:layout_weight="2">
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/menu_button_text"
android:background="@drawable/round_button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我看了这个问题,但即使我为相对布局本身分配了一个权重,我也无法为按钮分配权重.
可以在Android中同时查看两个xml吗?假设xml1包含ImageView,而xml2包含另一个ImageView.喜欢同时查看两个图像,共享窗口总大小的一半