我是android的新手,我希望能够做两件事:
我想要创建一组已经创建的视图,并且能够将它们放在java的其他活动中。
从java
创建其他视图答案 0 :(得分:0)
我刚刚创建了一个公共类和一些创建和返回视图的方法
包com.javanigga.lovershaters.lovershaters;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.TextPaint;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
/**
* Created by merli on 10/22/2017.
*/
public class views {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public TextView titulox(Context context, String texto)
{
TextView titulo;
titulo = new TextView(context);
titulo.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
titulo.setText(texto);
titulo.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
titulo.setTextSize(36);
titulo.setVisibility(View.VISIBLE);
return titulo;
}
public ImageView imagen (String src, Context context) throws IOException {
Bitmap bitmap = null;
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
}
ImageView imagen = new ImageView(context);
imagen.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 350));
imagen.setImageBitmap(bitmap);
imagen.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imagen;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public TextView desarrollo(Context context, String texto)
{
TextView desarrollo;
desarrollo = new TextView(context);
desarrollo.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
desarrollo.setText(texto);
desarrollo.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
desarrollo.setTextSize(14);
desarrollo.setVisibility(View.VISIBLE);
return desarrollo;
}
}