java - 如何动态设置布局参数到 fill_parent

标签 java android android-layout android-view

enter image description here 我的父线性布局

        <LinearLayout
        android:id="@+id/gif_linear_layout"
        android:background="@drawable/shape"
        android:layout_marginTop="5dp"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical" />

所以我有这个类 GifWebView

import android.content.Context;
import java.io.InputStream;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.os.SystemClock;
import android.view.View;

public class GifWebView extends View {
    private Movie mMovie;
    InputStream mStream;
    long mMoviestart;

    public GifWebView(Context context, InputStream stream) {
        super(context);
        mStream = stream;
        mMovie = Movie.decodeStream(mStream);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        super.onDraw(canvas);
        final long now = SystemClock.uptimeMillis();

        if (mMoviestart == 0) 
        {
            mMoviestart = now;
        }
        int gif_image_duration = mMovie.duration();
        if(gif_image_duration==0)
        {
            gif_image_duration = 1;
        }
        final int relTime = (int) ((now - mMoviestart) % gif_image_duration);
        mMovie.setTime(relTime);
        mMovie.draw(canvas, 10, 10);
        this.invalidate();
    }
}

我在我的主要 Activity 中创建此类的实例来显示 gif 图像 效果很好 在主要 Activity 中

GifWebView gif_view;//global var

// already existing linear layout in my layout file 

// and then this in onCreate() method

gif_linear_layout = (LinearLayout)findViewById(R.id.gif_linear_layout);

InputStream stream = null;
        try {
            stream = getAssets().open(gif_name);
            //stream = getAssets().open("gif_images").;
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

        gif_view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        gif_linear_layout.addView(gif_view);

现在有人请告诉我如何为通过创建 GifWebVIew 类的实例创建的 View 设置布局参数

实际上我希望实例 View 完全适合我的线性布局 但这不合适

请提供任何帮助。

最佳答案

// just replace your parent layout with this
 <LinearLayout
        android:id="@+id/gif_linear_layout"
        android:background="@drawable/shape"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

关于java - 如何动态设置布局参数到 fill_parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082663/

相关文章:

android - 计时器:我想让一个按钮重置为 "00.00"而不是重启计时器

java - 如何查找和替换子字符串?

Android Studio 渲染问题

java - 如何将 MVVM 与 App/activity 和 AsyncTask 的 UI 组件一起使用

java - JBoss 如何实现 HttpServletResponse setStatus() 与 sendError()

java - JaxB 读取类层次结构

android - Espresso 循环测试

java - 为什么对此 Treeset 的删除调用不能有效删除元素?

android - 更改微调器指示器可绘制

android - 动态设置线性布局背景