标签: android-relativelayout

如何用一个在另一个上面的两个按钮以编程方式创建RelativeLayout?

我在UI中添加了两个按钮,但它们显示在另一个上面.我希望它们彼此相邻.我在这段代码中缺少什么?

m_btnCrown = new ImageButton(this);
m_btnCrown.setImageResource(R.drawable.king_crown_thumb);
m_btnCrown.setAlpha(100);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);


addContentView(m_btnCrown, lp);


m_btnMonkey = new ImageButton(this);
m_btnMonkey.setImageResource(R.drawable.monkey_small);
m_btnMonkey.setAlpha(100);

lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.RIGHT_OF, m_btnCrown.getId());   

addContentView(m_btnMonkey, lp);
Run Code Online (Sandbox Code Playgroud)

android android-relativelayout

65
推荐指数
2
解决办法
11万
查看次数

Android:对齐父级底部+底部边距

我已经使用了相对布局,我想在屏幕底部设置按钮,但是这会把它全部放到底部,我想有一些余量,所以它在屏幕的末端之间有一些空间/查看和按钮.无论我做什么,按钮边距在2.1+上都没有做任何事情由于某种原因.相对布局包含一个背景,所以我不能但是它的余量.

有人知道解决这个问题吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:background="@drawable/background" >

    <Button
        android:id="@+id/confirm_mobile_button_next"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="false"
        android:layout_centerHorizontal="false"
        android:layout_centerInParent="false"
        android:layout_margin="15dp"
        android:background="@drawable/button_shape_selector"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:text="@string/confirm_mobile_no_continue"
        android:textColor="@color/white"
        android:textStyle="bold" />

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

android margins android-relativelayout

64
推荐指数
3
解决办法
8万
查看次数

如何以编程方式将视图添加到RelativeLayout?

你能给我一个非常简单的例子,RelativeLayout在给定的位置以编程方式添加子视图吗?

例如,要反映以下XML:

<?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">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="107dp"
    android:layout_marginTop="103dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />
Run Code Online (Sandbox Code Playgroud)

我不明白如何创建一个合适的RelativeLayout.LayoutParams实例.

android android-layout android-relativelayout

58
推荐指数
2
解决办法
8万
查看次数

FrameLayout与RelativeLayout进行叠加

我需要为我的应用程序实现一个覆盖(半透明)屏幕,类似于Showcase View

我的猜测是FrameLayout用于这个用例,因为它用于将项目堆叠在一起.但我很惊讶地发现上面的库使用了RelativeLayout.

我的问题是什么时候使用FrameLayout,如果不是这样的话?如果我走的FrameLayout路有什么缺点?

android android-relativelayout android-framelayout

58
推荐指数
1
解决办法
4万
查看次数

android相对布局低于2项

我有一个像这样的相对布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/single_row"
    android:padding="12dip">

    <ImageView
        android:id="@+id/page_image"
        android:layout_marginRight="6dip"
        android:layout_width="66dip"
        android:layout_height="66dip"
        android:layout_alignParentLeft="true"
        android:src="@drawable/no_photo" />
    <TextView
        android:id="@+id/page_name"
        style="@style/pulse_content"
        android:layout_alignTop="@id/page_image"
        android:layout_toRightOf="@id/page_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/page_desc"
        android:layout_below="@id/page_name"
        style="@style/pulse_content"
        android:layout_alignLeft="@id/page_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Principal Consultant" />
    <Button
        android:id="@+id/follow_button"
        android:layout_below="@id/author_image"
        android:layout_marginTop="15dip"
        android:layout_alignParentBottom="true"
        android:text="Follow"
        style="@style/follow_button" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是我希望follow_button低于page_desc和page_image.有时,page_desc内容的高度会大于图像的大小,有时则不会.问题是,如果我将follow_button设置在图像或描述下面,那么另一个将被剪裁.是否有一种有效/有效的方法来确保图像或page_desc始终可见并位于按钮上方?

android android-relativelayout

53
推荐指数
2
解决办法
8万
查看次数

无法在ScrollView内调整RelativeLayout以填充整个屏幕

我有一个奇怪的问题,我不知道如何解决.我在ScrollView中有一个RelativeLayout,这个scrollView的高度设置为fill_parent.RelativeLayout也是如此.尽管如此,内容并没有填满整个屏幕,它只会进入此RelativeLayout中最后一个布局的wrap_content.

这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.pontai"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/relativeLayout3"
                android:layout_alignParentTop="true"
                android:layout_alignRight="@+id/relativeLayout5"
                android:layout_marginTop="5dp"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="How much points you have here:"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <TextView
                    android:id="@+id/textViewTotalPoints1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="120"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/relativeLayout4"
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:layout_alignLeft="@+id/relativeLayout3"
                android:layout_below="@+id/linearLayout1"
                android:layout_marginTop="5dp"
                android:layout_toLeftOf="@+id/textView7"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="Ratings"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/textViewPositivePoints"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="25dp" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-scrollview android-relativelayout

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

以编程方式创建一个新的TextView,然后将其显示在另一个TextView下面

String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
    TextView tv=new TextView(getApplicationContext());
    tv.setText(textArray[i]);
    relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
    layout.addView(tv, relativeParams);
}
Run Code Online (Sandbox Code Playgroud)

我需要做那样的事情......所以它会显示为

one
two
asdfasdfsomething
Run Code Online (Sandbox Code Playgroud)

屏幕上..

java android textview android-relativelayout

46
推荐指数
4
解决办法
17万
查看次数

如何在Android Studio中从默认的ConstraintLayout切换到RelativeLayout

我有最新的Android Studio(2.3 beta 3),看起来ConstraintLayout是创建项目时的默认设置.如何让Android Studio使用RelativeLayout作为新项目的默认布局元素?

在此输入图像描述

android android-studio android-relativelayout android-constraintlayout

44
推荐指数
7
解决办法
9万
查看次数

以编程方式设置android:layout_centerHorizo​​ntal

在xml中,您可以执行以下操作:

<TextView
    ...
    android:layout_centerHorizontal="true"
    ...
/>
Run Code Online (Sandbox Code Playgroud)

当我有实例时,我将如何以TextView编程方式执行此操作?

android centering android-layout android-relativelayout

43
推荐指数
2
解决办法
3万
查看次数

ImageView填充父级的宽度或高度,但保持纵横比

我有一个方形图像(虽然这个问题也适用于矩形图像).我想尽可能大地显示图像,必要时拉伸它们以填充它们的父母,同时仍然保持纵横比.图像小于ImageView.问题是,我无法拉伸图像并"匹配"ImageView的高度和宽度.

这是我的XML布局文件:

<?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="wrap_content"
                android:padding="10dp">

    <ImageView android:id="@+id/image"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:adjustViewBounds="true"
               android:scaleType="fitCenter"
               android:layout_marginTop="10dp"/>

    <TextView android:id="@+id/name"
              android:layout_below="@id/image"
              android:layout_alignLeft="@id/image"
              android:layout_marginTop="20dp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textSize="18dp"/>

    <TextView android:id="@+id/name2"
              android:layout_below="@id/name"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textSize="14dp"/>


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

我已经使用的多种组合fill_parent,wrap_content与多个scaleTypes: ,fitCenter,fitStart,fitEnd,centerInside他们都画在正确的纵横比的图像,但没有人真正缩放图像UP和ImageView的本身,导致无论是在TextViews被推所有屏幕下方,ImageView内部空白区域,图像未缩放或图像裁剪.

我无法为此找到合适的组合.

android android-layout android-imageview android-relativelayout

41
推荐指数
4
解决办法
6万
查看次数