标签: android-relativelayout

以编程方式将属性AlignParentRight设置为RelativeLayout中的按钮不起作用?

我尝试以编程方式将属性AlignParentRight设置为RelativeLayout中的按钮,但程序崩溃时出现NullPointerException.这是我的代码:

//add horizontal realative layout
RelativeLayout layouth = new RelativeLayout(this);
layouth.setLayoutParams(new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

//add textview for label
TextView t = new TextView(this);
t.setText(d.getName());
t.setTextSize(25);
t.setId(2);      
t.setLayoutParams(new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

//add button
Button b = new Button(this);
b.setText(d.getName());
RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)b.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
layoutParams.addRule(RelativeLayout.LEFT_OF, 2);
b.setLayoutParams(layoutParams);   

//add textview and button to horizontal linear layout
layouth.addView(t);
layouth.addView(b);
//add horizontal linear layout to vertical linear layout
layoutv = (LinearLayout) findViewById(R.id.ProjectViewLinearID);
layoutv.addView(layouth);
Run Code Online (Sandbox Code Playgroud)

我的问题在哪里?该错误发生在程序行中layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

更新解决方案:

这段代码适合我:

// Creating a new RelativeLayout
RelativeLayout layouth = …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-button android-relativelayout

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

RelativeLayout:对齐视图相对于其他视图居中水平或垂直

是否可以根据另一个已存在的视图将相对于中心水平或垂直的RelativeLayout中的视图对齐.

例如:假设有这样的事情:

在此输入图像描述

第二个文本视图应显示在第一个文本视图下方的中心:

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="72dp"
        android:text="dynamic text" />

    <TextView
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_marginLeft="43dp" <!-- Is there a rule to center it? -->
        android:text="centered below text 1" />

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

有可能在XML中实现类似的东西吗?有没有我错过的规则?我不想以编程方式计算位置

android center android-relativelayout

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

相对布局中的z-index

我需要在RelativeLayout中的Button上方放置一个TextView.但它不起作用:TextView总是在Button下,即使我在按钮之前移动它.我也尝试过bringChildToFront()在前面移动textview的功能,但没有运气.

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

<Button
    android:id="@+id/bVio"
    android:layout_width="wrap_content"
    android:layout_height="50dip"
    android:layout_alignParentLeft="true"
    android:text="VIO"></Button>

    <TextView
        android:id="@+id/bVio_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/bVio"
        android:layout_marginTop="6dip"
        android:layout_marginRight="6dip"
        android:text="00"
        android:textSize="15dip"
        android:textColor="#FF0000"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

android z-index android-relativelayout

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

自定义视图的onMeasure:如何根据高度获取宽度

我的问题的先前版本太冗长了.人们无法理解它,所以以下是完全重写.如果您对旧版本感兴趣,请参阅编辑历史记录.

一个RelativeLayout家长把MeasureSpec小号到其子视图的onMeasure方法才能看到孩子想有多大是.这发生在几次通过中.

我的自定义视图

我有一个自定义视图.随着视图内容的增加,视图的高度也会增加.当视图达到父级允许的最大高度时,视图的宽度会随着任何其他内容的增加而增加(只要为wrap_content宽度选择).因此,自定义视图的宽度直接取决于父级所说的最大高度必须是什么.

在此输入图像描述

(不和谐的)父母对话

onMeasure 通过1

RelativeLayout家长告诉我的观点,"你可以是任何宽度达到 900任何高度可达 600.你说什么?"

我的观点是,"嗯,在那个高度,我可以适应所有宽度的东西100.所以我要宽度100和高度600."

onMeasure 通过2

RelativeLayout家长告诉我的观点,"你告诉我,你想要的宽度最后一次100,所以我们设置为一个确切的宽度.现在,基于这样的宽度,你会喜欢什么样的高度?凡是 500正常."

"嘿!" 我的观点回复."如果你只给我一个最大的高度500,那么100就太窄了.我需要一个200高度的宽度.但很好,按你的方式行事.我不会违反规则(但......).我将采取宽度100和高度500."

最后结果

所述RelativeLayout父分配图的最终尺寸100的宽度和500对高度.对于视图来说,这当然太窄了,部分内容会被剪裁.

"叹气,"我的看法."为什么我的父母不会让我更宽?有足够的空间.也许Stack Overflow上的某个人可以给我一些建议."

android android-custom-view onmeasure android-relativelayout

17
推荐指数
1
解决办法
6105
查看次数

以编程方式将项添加到相对布局

我一直在寻找这个问题的答案.我是Android的新手,并尝试通过java而不是xml以编程方式将项目添加到相对布局.我已经创建了一个测试类来尝试它,但项目保持堆叠而不是正确格式化.我现在只想在另一个下面使用一个TextView(最终我将使用参数的左侧和右侧,但我开始很简单.我缺少什么?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScrollView sv = new ScrollView(this);
    RelativeLayout ll = new RelativeLayout(this);
    ll.setId(99);
    sv.addView(ll);
    TextView tv = new TextView(this);
    tv.setText("txt1");
    tv.setId(1);
    TextView tv2 = new TextView(this);
    tv2.setText("txt2");
    tv2.setId(2);
    RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    ll.addView(tv, lay);
    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
    ll.addView(tv2, p);  this.setContentView(sv);};
Run Code Online (Sandbox Code Playgroud)

android android-layout android-relativelayout

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

设置RelativeLayout子级以填充未使用的空间

我有这个布局:

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/stat_brightness_off"
        android:layout_toLeftOf="@id/brightnessSeekBar" />

    <SeekBar
        android:id="@+id/brightnessSeekBar"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/brightnessSeekBar"
        android:src="@drawable/stat_brightness_on" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我需要SeekBar填充所有未使用的水平空间ImageView(它们大约是64*64像素).我该怎么办?

android android-layout android-relativelayout

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

为什么我的RelativeLayout中的ImageView顶部和底部会出现空白区域?

我使用relativelayout覆盖图像.到目前为止我所测试的所有屏幕尺寸(从2.7到10.1英寸)我总是在我的图像上方获得空白区域.在我的IDE中,我总是看到我的relativelayout导致了图像顶部和底部的额外空间.

这是为什么?我已将所有高度属性设置为wrap_content甚至添加了adjustViewBounds属性.

注意:您应该知道我的图像尺寸更大,这意味着会有某种缩放.

谢谢你的提示!

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical" >

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical" 
    android:focusable="true"
android:focusableInTouchMode="true">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/bgf"
    android:textColor="#000000"
    android:textSize="25sp" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:adjustViewBounds="true"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:paddingBottom="5dp"
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/cde"
        android:contentDescription="@string/cde" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
        android:src="@drawable/fgh"
        android:contentDescription="@string/abc" />

</RelativeLayout>


</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

android android-linearlayout android-relativelayout

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

RelativeLayout高度填充剩余空间

我的xml文件中有以下布局:

<RelativeLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent">

    <FrameLayout android:id="@+id/logoLayout"  
                 android:layout_width="fill_parent"
             android:layout_height="wrap_content">
          -- some images
    </FrameLayout>


    <RelativeLayout android:layout_width="fill_parent" 
                    android:layout_height="wrap_content" 
                    android:gravity="center" 
                    android:orientation="vertical"
                    android:layout_below="@+id/logoLayout">

               Button 1
               Button 2
               Button 3
               Button 4

    </RelativeLayout>

    <RelativeLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:layout_alignParentBottom="true">

                 Button 5
    </RelativeLayout>

<RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

也许我没有以最好的方式做到这一点.我想要的是:让包含4个按钮的布局使用顶部和底部布局之间的整个空间,并且我希望按钮在布局中平行排列.

像这样:http://img16.imageshack.us/i/androidq.png/

我添加了整个布局代码:

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


    <!--The header of the page-->
        <FrameLayout android:id="@+id/logoLayout" 
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content">        

             <ImageView android:id="@+id/logoBackground" 
                        android:src="@drawable/logo_background_small"
                        android:layout_width="fill_parent" 
                        android:layout_height="wrap_content"/>

             <ImageView android:id="@+id/logoImage" 
                        android:src="@drawable/logo_small"
                        android:layout_width="wrap_content" 
                        android:layout_height="wrap_content" 
                        android:layout_gravity="left"
                        android:gravity="center"
                        android:padding="3dip"/>  

             <TextView android:layout_width="fill_parent" 
                       android:layout_height="wrap_content" 
                       android:text="@string/tracks"
                       android:layout_gravity="center"
                       android:gravity="right"
                       android:textSize="22dip" …
Run Code Online (Sandbox Code Playgroud)

android android-relativelayout

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

Android RelativeLayout选择器state_pressed不起作用

RelativeLayout喜欢这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/contacts"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="0.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:contentDescription="@string/content_description_contacts"
    android:scaleType="fitXY"
    android:src="@drawable/contacts" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignBottom="@id/image"
    android:paddingBottom="10dp"
    android:textColor="@drawable/text_color"
    android:text="@string/button_contacts"
    android:textSize="12sp" />    
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

看起来像:

在此输入图像描述

我的contacts选择器似乎:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
    android:drawable="@drawable/contacts_over" />
<item android:state_selected="true"
    android:drawable="@drawable/contacts_selected" />
<item
     android:drawable="@drawable/contacts_default" />
</selector>
Run Code Online (Sandbox Code Playgroud)

如你所见,我有3张图片:默认情况下,选中并按下.

但我有一个问题:只有默认和state_selected图像按预期工作,但state_pressed似乎没有用.

我有几个上面提到的RelativeLayouts,没有人合作state_pressed.

有人知道我的问题在哪里吗?

谢谢!

android android-relativelayout

15
推荐指数
3
解决办法
9833
查看次数

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

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

例:

在此输入图像描述

android android-layout android-relativelayout

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