我正在为Activity
我的Android应用程序中的登录工作.下面的图片是我希望它看起来像:
我能够使用以下XML实现此布局.问题是,它有点hackish.我不得不为主机EditText硬编码宽度.具体来说,我必须指定:
android:layout_width="172dp"
Run Code Online (Sandbox Code Playgroud)
我真的想给主机和端口EditText提供一个百分比宽度.(主机为80%,端口为20%.)这可能吗?以下XML适用于我的Droid,但似乎并不适用于所有屏幕.我真的想要一个更强大的解决方案.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/host_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/home"
android:paddingLeft="15dp"
android:paddingTop="0dp"
android:text="host"
android:textColor="#a5d4e2"
android:textSize="25sp"
android:textStyle="normal" />
<TextView
android:id="@+id/port_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/home"
android:layout_toRightOf="@+id/host_input"
android:paddingTop="0dp"
android:text="port"
android:textColor="#a5d4e2"
android:textSize="25sp"
android:textStyle="normal" />
<EditText
android:id="@+id/host_input"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_below="@id/host_label"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="4dp"
android:background="@android:drawable/editbox_background"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/port_input"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@id/host_label"
android:layout_marginTop="4dp"
android:layout_toRightOf="@id/host_input"
android:background="@android:drawable/editbox_background"
android:inputType="number" />
<TextView
android:id="@+id/username_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/host_input"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="username"
android:textColor="#a5d4e2"
android:textSize="25sp"
android:textStyle="normal" />
<EditText
android:id="@+id/username_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/username_label"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="4dp"
android:background="@android:drawable/editbox_background" …
Run Code Online (Sandbox Code Playgroud) 我有一个相对布局,我正在以编程方式创建:
RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Run Code Online (Sandbox Code Playgroud)
现在我有两个按钮,我想在这个相对布局中添加.但问题是RelatiiveLayout左侧的两个按钮都显示在彼此之间.
buttonContainer.addView(btn1);
buttonContainer.addView(btn2);
Run Code Online (Sandbox Code Playgroud)
现在我想知道如何以编程方式设置按钮的android:layout_alignParentRight="true
"或 android:layout_toLeftOf="@id/btn"
属性",就像我们在xml中一样?
我试图以编程方式实现以下(而不是通过XML声明):
<RelativeLayout...>
<TextView ...
android:id="@+id/label1" />
<TextView ...
android:id="@+id/label2"
android:layout_below: "@id/label1" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
换句话说,如何使第二个TextView
出现在第一个下面,但我想在代码中执行:
RelativeLayout layout = new RelativeLayout(this);
TextView label1 = new TextView(this);
TextView label2 = new TextView(this);
...
layout.addView(label1);
layout.addView(label2);
setContentView(layout);
Run Code Online (Sandbox Code Playgroud)
谢谢,TreeUK.我理解大方向,但它仍然不起作用 - "B"重叠"A".我究竟做错了什么?
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
Run Code Online (Sandbox Code Playgroud) 我想在Android中定义RelativeLayout视图的z顺序.
我知道这样做的一种方法是打电话bringToFront
.
有更好的方法吗?如果我可以在布局xml中定义z顺序会很棒.
我对ConstraintLayout和RelativeLayout之间的区别感到困惑.有人可以告诉我他们之间的确切差异吗?
android android-layout android-relativelayout android-constraintlayout
是否可以在运行时创建RelativeLayout来以android:layout_below
编程方式设置等效项?
我对LinearLayout,RelativeLayout和AbsoluteLayout之间的区别感到困惑.有人可以告诉我他们之间的确切差异吗?
android android-layout android-linearlayout android-relativelayout android-framelayout
我有一个像这样的RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip">
<Button
android:id="@+id/negativeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="#ffffff"
android:layout_alignParentLeft="true"
android:background="@drawable/black_menu_button"
android:layout_marginLeft="5dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/positiveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:background="@drawable/blue_menu_button"
android:layout_marginRight="5dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我希望能够以编程方式设置positiveButton
相同的效果:
android:layout_centerInParent="true"
Run Code Online (Sandbox Code Playgroud)
我怎样才能以编程方式进行此操作?
我一直在尝试了几天,现在让我的布局更有效率使用的几个层次转换嵌套LinearLayouts
一个RelativeLayout
和所遇到的一些问题,我的天堂没有能够找到一个解决方法...
我搜索了Android初学者小组和这个网站,但却找不到任何可以帮我解决问题的方法.
我在其中一个博客上看到,您可以将布局与合并和包含标签结合起来.所以我拥有一个带有RelativeLayout
根元素的主布局文件.在其中我有5个包含标签,它们引用了5个不同的xml布局文件,每个文件都有一个根的合并元素(我的所有合并文件都是相同的,除了它们中的ID).
我遇到了两个问题,我将在发布布局代码的简化版后解释:
示例主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/translucent_gray" >
<include
android:id="@+id/running_gallery_layout_id"
layout="@layout/running_gallery_layout" />
<include
android:id="@+id/recent_gallery_layout_id"
layout="@layout/recent_gallery_layout"
android:layout_below="@id/running_gallery_layout_id" />
<include
android:id="@+id/service_gallery_layout_id"
layout="@layout/service_gallery_layout"
android:layout_below="@id/recent_gallery_layout_id" />
<include
android:id="@+id/process_gallery_layout_id"
layout="@layout/process_gallery_layout"
android:layout_below="@id/service_gallery_layout_id" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
示例包括合并文件:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
style="@style/TitleText"
android:id="@+id/service_gallery_title_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/service_title" />
<Gallery
android:id="@+id/service_gallery_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_below="@id/service_gallery_title_text_id" />
<TextView
style="@style/SubTitleText"
android:id="@+id/service_gallery_current_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/service_gallery_title_text_id"
android:layout_above="@id/service_gallery_id" />
</merge>
Run Code Online (Sandbox Code Playgroud)
我遇到两个问题:
1)android:layout_*
在include标记中使用时,似乎忽略了属性,并且所有合并的布局都显示在彼此之上.根据这篇文章(http://developer.android.com/resources/articles/layout-tricks-reuse.html)"任何android:layout_*
属性都可以与<include /> …
每次我需要一个View容器时,我一直在使用RelativeLayout,因为它的灵活性,即使我只是想显示一些非常简单的东西.
这样做是否可以,或者我应该从性能/良好实践的角度尝试使用LinearLayout?
谢谢!
performance android android-linearlayout android-relativelayout