Android 制作RelitiveLayouot视图
我是android新手,所以我花了好几个小时试图在代码中使其看起来像这样。有人能帮忙吗 以下是我的尝试:Android 制作RelitiveLayouot视图,android,android-layout,Android,Android Layout,我是android新手,所以我花了好几个小时试图在代码中使其看起来像这样。有人能帮忙吗 以下是我的尝试: RelativeLayout photo = new RelativeLayout(this); photo.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, densityToPixels(80))); photo.setGrav
RelativeLayout photo = new RelativeLayout(this);
photo.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, densityToPixels(80)));
photo.setGravity(Gravity.CENTER_VERTICAL);
allphotos.addView(photo);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView1.setGravity(Gravity.CENTER_VERTICAL);
textView1.setText("IDasdfasdfasdfasdfasdfd");
photo.addView(textView1);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
ImageView img = new ImageView(this);
img.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
img.setLayoutParams(params);
photo.addView(img);
ImageView del_img = new ImageView(this);
img.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
img.setLayoutParams(params);
photo.addView(del_img);
你为什么用代码做这些?使用xml。如果需要从xml构建视图,请使用
视图。充气将xml传递给视图。在代码中确实不需要这样做…
只需使用TextView并向其添加两个复合绘图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/txtText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="8dp"
android:textSize="14sp"
android:textColor="@color/white"
android:drawableLeft="@drawable/icon_left"
android:drawableRight="@drawable/icon_rite"
android:drawablePadding="8dp"
/>
</RelativeLayout>
2-您需要在代码中设置可提取项:
// given that you retrieved your TextView as txt and you retrieved your drawables as drwLeft and drwRite
// Parameter order: left, top, right, bottom
txt.setCompoundDrawablesWithIntrinsicBounds(drwLeft, null, drwRite, null);
更详细:我正在制作照片列表。每次我拍一张照片,它都用这种方式把它放下来。所以我发现这是最简单的方法。然后使用ListActivity并使ListAdapter的getView返回(或重用)视图。膨胀(您的xml)听起来很复杂,您知道有没有一个例子?这一个似乎与您正在做的类似,我试着这样做。但在我拍摄后找不到将照片附加到列表视图的方法我需要插入拍摄后拍摄的照片。可能吗?如何更改此代码?但如何更改?我真的不知道
// given that you retrieved your TextView as txt and you retrieved your drawables as drwLeft and drwRite
// Parameter order: left, top, right, bottom
txt.setCompoundDrawablesWithIntrinsicBounds(drwLeft, null, drwRite, null);