使用大量字段创建相对布局(以编程方式)

时间:2015-02-16 14:30:28

标签: java android android-activity layout programmatically-created

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

现在,即使我将参数传递到标题顶部,所有组件都会进入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"));
    }
}

我的活动xml很简单......

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

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

EDITED: 我输入了param this.addContentView(layout,lp);现在它的工作原理。 需要很多帮助。

修改第一个代码后,现在返回的是白色屏幕。 我拿出原始代码的一些部分,所以我会更好地看到正在做的事情(对或错)。

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);
    }
}