标签: android-relativelayout

Android问题将带有子项的ViewGroup转换为位图

我正在使用Canvas将ViewGroup(RelativeLayout)成功转换为Bitmap.但是,当绘制发生时,我只看到ViewGroup的背景可绘制而不是它的子(两个TextViews)应该使用像FILL_PARENT这样的规则在RelativeLayout中布局.

RelativeLayout使用以下静态函数创建:

public static RelativeLayout createProgrammeView(Context context, int width, int height, String title, String time) {
    RelativeLayout.LayoutParams params;

    // Layout Root View (RelativeLayout)
    RelativeLayout rlv = new RelativeLayout(context);
    params = new RelativeLayout.LayoutParams(width, height);
    rlv.setLayoutParams(params);
    rlv.setPadding(3, 3, 3, 3);
    rlv.setBackgroundResource(R.drawable.background);

    // Layout Title
    TextView tv = new TextView(context);
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    tv.setId(R.id.title);
    tv.setLayoutParams(params);
    tv.setGravity(Gravity.CENTER_VERTICAL);
    tv.setSingleLine(true);
    tv.setEllipsize(TruncateAt.END);
    tv.setTextColor(Color.parseColor("#fff"));
    tv.setTextSize(11);
    tv.setText(title);
    rlv.addView(tv);

    // Layout Start Time
    tv = new TextView(context);
    params = new RelativeLayout.LayoutParams(16, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW, R.id.title);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); …
Run Code Online (Sandbox Code Playgroud)

android bitmap android-layout android-relativelayout

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

RelativeLayout添加规则"RelativeLayout.LEFT_OF"不起作用

我有一个relativeLayout如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:id="@+id/parent" >

    <ListView 
        android:layout_width="360dp"
        android:layout_height="600dp"
        android:id="@+id/list"
        android:inputType="text"
        android:maxLines="1"
        android:layout_margin="50dp"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在java代码中,我想在listview的左侧添加一个视图,但它没有用:

m_relativeLayout = (RelativeLayout)findViewById(R.id.parent);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.LEFT_OF, m_listView.getId());
Button button2 = new Button(this);
button2.setText("I am button 2");
m_relativeLayout.addView(button2, layoutParams);
Run Code Online (Sandbox Code Playgroud)

只有当我将listview设置为alignParentRight时,它才会起作用.这是一个机器人的bug还是我错过了什么?

我总是尝试addView(View child, int index, LayoutParams params),但它可能只适用于线性布局.那么有一个正常的解决方案来完成RelativeLayout.LEFT_OF工作吗?

编辑

我曾尝试RelativeLayout.BELOWRelativeLayout.RIGHT_OF,他们的工作完美,所以这意味着我没有足够的地方,以获得该按钮?我试图给予更多空间,但它仍然不起作用.

我使用东芝AT100(1280*800)和风景,所以空间足够.测试below和和.right一样left.我想如果我把一个控制器A放在relativelayout中,那么我在控制器A的左边添加控件B和decalare,结果应该是控件B将控件A推到右边,对吗?

android android-layout android-relativelayout

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

Android:以编程方式对齐父级底部+底部边距

如何以编程方式添加与父级底部对齐的RelativeLayout,并在同一RelativeLayout中添加边距或填充?

例:

在此输入图像描述

android android-layout android-relativelayout

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

相对布局alignParentLeft vs alignParentStart

所以我对使用相对布局很满意,但是在习惯Android Studio时我注意到在我的相对布局子视图中它生成了以下两种情况.

android:layout_alignParentLeft="true"
android:layout_alignParentStart="true
Run Code Online (Sandbox Code Playgroud)

在这里检查了Android文档,但看不出两者之间的区别.当然在Android Studio中相互交换显示没有明显的区别.有吗?

android android-relativelayout

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

RelativeLayout - 向下滚动

我有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+navigate/RLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ABABAB"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textStyle="bold"
        android:id="@+id/myid" 
        android:layout_centerHorizontal="true"
        android:src="@drawable/myid"
        android:layout_marginTop="15dp" />.
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:orientation="vertical"
        android:id="@+id/firstRectangle"
        android:background="@drawable/bg"
        android:layout_below="@+id/myid" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textStyle="bold|italic"
        android:textColor="#000000"
        android:id="@+id/hotelinfos"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_below="@+id/myid"
        android:layout_alignLeft="@+id/myid"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:id="@+id/hotelname"
        android:layout_below="@+id/hotelinfos"
        android:layout_alignLeft="@+id/hotelinfos" 
        android:layout_marginTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textColor="#000000"
        android:id="@+id/hoteladdress"
        android:layout_below="@+id/hotelname"
        android:layout_alignLeft="@+id/hotelname" 
        android:layout_marginTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:id="@+id/hotelphone"
        android:layout_below="@+id/hoteladdress"
        android:layout_alignLeft="@+id/hoteladdress" 
        android:layout_marginTop="10dp"
        android:textColor="#12C"
        android:textStyle="bold|italic"
        android:onClick="onClick"
        android:clickable="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp" …
Run Code Online (Sandbox Code Playgroud)

android android-scrollview android-relativelayout

14
推荐指数
1
解决办法
3万
查看次数

如何使用RelativeLayout使视图居中?

我想知道如何使用RelativeLayout将View放在两个其他视图之间(或在视图和父边缘之间).

例如,如果我有以下...

在此输入图像描述

如何使用RelativeLayout将Button垂直居中在ImageView和屏幕底部?

我正在寻找一个解决方案......

  • Button没有以任何方式拉伸
  • 没有嵌套布局

我正在尝试在XML布局中执行此操作(不是以编程方式).

android android-layout android-relativelayout

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

如何以编程方式设置layout_alignParentRight ="false"?

如何以android:layout_alignParentRight="false"编程方式设置?我可以true通过此代码将其设置为但我想以false编程方式将其设置为:

RelativeLayout.Layoutparams params =
(RelativeLayout.LayoutParams)myView.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

有一个方法removeRule(int)但它被添加(API级别17).我想在API级别7或8中使用类似的东西.

android android-relativelayout android-layoutparams

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

Android以高度为百分比排列布局

我对Android和练习都很陌生.我正在尝试设计一个屏幕.它将包含一个背景图像和一个带滑动菜单的浮动容器.(有关详细信息,请参阅附图)

我的布局包括一个背景图片,一个带有一些图标的容器,这些图标浮在底部,但底部有一些边缘(见附图)

据我所知,这可以通过在底部安排"相对布局"并在其中放置图像来实现.这是对的吗 ?

另外,我想添加一个重复的透明图像作为浮动div的背景.

请给我一个很好的建议或指点我一个很好的教程

提前致谢

在此输入图像描述

android android-layout android-relativelayout

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

设置relativelayout的宽度为屏幕宽度的1/3?

我有一个如下的布局.现在,我不想将相对布局的宽度设置为固定240 dp.我想将相对布局的宽度设置为屏幕宽度的1/3.是否可以在xml文件中执行此操作.如果不可能,我怎么能用java代码实现呢?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/fullscreen"
        style="@style/translucent">
           <RelativeLayout
            android:layout_width="240dp"
            android:layout_height="fill_parent"
            android:layout_gravity="right"
            android:gravity="center"
            android:background="#88000000"
            android:id="@+id/sidebar">

           </RelativeLayout>

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

android width android-layout android-relativelayout

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

如何在RelativeLayout中强制执行循环引用?

我在RelativeLayout中有两个必须互相引用的小部件.从技术上讲,它不是循环引用,因为小部件A与小部件B垂直对齐,小部件B与小部件A水平对齐.这是我的代码(浓缩):

    <Button android:id="@+id/btnLanguageFrom"
        android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/imgArrow"
            android:text="English" />

    <ImageView android:id="@+id/imgArrow"
        android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp" 
            android:layout_alignParentTop="true"
            android:layout_alignBottom="@id/btnLanguageFrom"
            android:layout_centerHorizontal="true"
            android:src="@drawable/arrow_right" />
Run Code Online (Sandbox Code Playgroud)

但是当我构建时,我收到此错误:

错误:找不到与给定名称匹配的资源(在'layout_toLeftOf'中,值为'@id/imgArrow').

有趣的是,Eclipse中的Graphical Layout视图正确显示它并且不会抱怨循环引用.

我没有看到两个小部件沿不同维度(水平和垂直)相互引用的问题,因为它不会导致无限循环.有没有解决这个问题的方法?这是我知道获得我需要的布局的唯一方法.

在此先感谢,Barry PS有没有办法提前声明一个id,就像在C/C++中一样?

android android-relativelayout

13
推荐指数
1
解决办法
1405
查看次数