【问题标题】:How to change layout height programmatically in Android如何在 Android 中以编程方式更改布局高度
【发布时间】:2014-09-24 14:53:13
【问题描述】:

我尝试以编程方式更改 RelativeLayout 高度。我试图解释我的问题:在我的activity.xml 文件中,我有标题布局(顶部位置),并且我在底部位置也有一个布局。通过单击,我尝试更改 botton 位置布局的高度(全屏 - 顶部布局高度)。

这里是activity.xml代码:

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

<RelativeLayout
    android:id="@+id/movies_title_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/headerimage" >

    <ImageView
        android:id="@+id/gomoviewlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:background="@drawable/slide_menu_image" />
</RelativeLayout>

<ImageView
    android:id="@+id/play_trailer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/play_trailer" />

<RelativeLayout
    android:id="@+id/popaplayout"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:background="@drawable/movie_info_shadow" >

    <TextView
        android:id="@+id/movies_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="14dp"
        android:text="TextView"
        android:textColor="#ffffff"
        android:textSize="22dp" />

    <TextView
        android:id="@+id/category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/movies_title"
        android:layout_below="@+id/movies_title"
        android:layout_marginTop="16dp"
        android:text="TextView"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/descraption"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/category"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="5dp"
        android:text="TextView"
        android:textColor="#ffffff"
        android:textSize="20dp" />
</RelativeLayout>

这里是 Java 代码:

popaplayout.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT-movies_header_layout.getWidth()  );

            popaplayout.setLayoutParams(layout_description);

        }
    });

popaplayout 是我试图改变它的高度的布局。

我该如何解决我的问题?

【问题讨论】:

  • 你为什么要RelativeLayout.LayoutParams.MATCH_PARENT-movies_header_layout.getWidth()?你知道RelativeLayout.LayoutParams.MATCH_PARENT 的值总是-1 对吗?
  • 我尝试改变我的 popaplayout 高度。我的意思是。我的 popap 布局应该是波纹管标题布局
  • 什么是标题布局?

标签: android android-relativelayout


【解决方案1】:

将此添加到您的 onClickListener:

((RelativeLayout.LayoutParams)popaplayout.getLayoutParams()).addRule(RelativeLayout.BELOW, R.id.movies_title_layout);

这将使视图的顶部与标题的底部相同

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2018-03-17
    • 1970-01-01
    • 2015-09-30
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    相关资源
    最近更新 更多