创建具有大量字段的相对布局(以编程方式)

Creating relative layout (programmatically) with lot of fields

我正在尝试使用 java 创建相对布局。我会在顶部插入标志,在页脚上分享,在中间插入一些信息。

目前,所有组件都在 FOOTER 上,即使我将参数传递给页眉也是如此。

我的java代码是:

package com.clubee.vote;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ResultadoFalho extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resultadofalho);

    Bitmap bitmapTop = BitmapFactory.decodeResource(getResources(),R.drawable.bkg_app);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sharing);

    RelativeLayout layoutLogo = (RelativeLayout) findViewById(R.id.ibresultadoFalho);
    RelativeLayout.LayoutParams paramsLogo = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    ImageView bkgLogo = new ImageView(this);
    bkgLogo.setLayoutParams(paramsLogo);
    bkgLogo.setImageBitmap(bitmapTop);

    layoutLogo.setGravity(Gravity.TOP| Gravity.CENTER_VERTICAL);
    layoutLogo.addView(bkgLogo);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.ibresultadoFalho);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    ImageButton sharingButton  = new ImageButton(this);
    sharingButton.setLayoutParams(params);
    sharingButton.setImageBitmap(bitmap);

    layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.BOTTOM);
    layout.addView(sharingButton);

    sharingButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            shareIt();
        }
    });
}

private void shareIt() {
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = "Eu votei! E você, já opinou sobre a atual gestão da presidente do Brasil?";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Vote, Opine, Compartilhe");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Compartilhar"));
    }
}

我的activityxml很简单...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ibresultadoFalho"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#fffcfffa">


</RelativeLayout>

您需要使用 RelativeLayout.LayoutParams.

还可以在 SO 上查看这些其他问题,

已编辑: 我输入参数 this.addContentView(layout, lp);现在它起作用了。 非常感谢 4 个帮助。

修改第一段代码后,现在return是白屏。 我提取了原始代码的一些部分,所以我可以更好地了解正在做什么(对或错)。

package com.clubee.vote;

import android.app.Activity;    
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ResultadoFalho extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resultadofalho);

    RelativeLayout layout = new RelativeLayout(this);

    Bitmap bitmapTop =  BitmapFactory.decodeResource(getResources(),R.drawable.bkg_app);

    ImageView bkgLogo = new ImageView(this);
    bkgLogo.setId('1');
    bkgLogo.setImageBitmap(bitmapTop);


    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    layout.setLayoutParams(lp);

    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, bkgLogo.getId());

    layout.addView(bkgLogo);
    }
}