以编程方式添加的相对布局不会显示
Relative Layout added programmatically won't show
我正在尝试以编程方式添加特定的相对布局 xml。它由一个 ImageView、EditText 和一个 TextView 组成。问题似乎是我试图在从另一个 activity 返回后在 "onActivityResult" 方法上执行此操作。当我在其他地方这样做时它可以工作并且我想创建的布局正确显示(例如我成功尝试 "onCreate" 并按下按钮),但如果在 "onActivityResult" 内则不起作用。我需要它在那里。
这两种情况打印都输出成功,没有报错和崩溃,就是排版不显示。任何建议表示赞赏。
这是我要创建的 xml 文件:
<?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="260dip">
<ImageView
android:id="@+id/damagePicture"
android:layout_width="match_parent"
android:layout_height="210dip"
android:scaleType="centerCrop"
android:layout_marginTop="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:src="@drawable/nocar"/>
<EditText
android:id="@+id/orderTextfield"
android:layout_height="40dip"
android:layout_width="match_parent"
android:gravity="left|center"
android:layout_marginLeft="10dip"
android:layout_marginRight="45dip"
android:hint="Enter damage description"
android:textSize="14sp"
android:layout_alignParentLeft="true"
android:textColor="@color/black"
android:textColorHint="@color/gray_vin_search_text"
android:background="@null"
android:singleLine="true"
android:editable="true"
android:layout_below="@+id/damagePicture"/>
<TextView
android:id="@+id/removePicture"
android:layout_width="40dip"
android:layout_height="40dip"
android:text="@string/discardPicture"
android:gravity="center|center"
android:textColor="@color/black"
android:textSize="24sp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:clickable="true"
android:onClick="removePicture"/>
<View
android:id="@+id/bottomLine"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_alignParentBottom="true"
android:background="@color/gray_for_line"></View>
</RelativeLayout>
这里是代码的相关部分:
public void addResultingDamagePicture(String pathToFile) {
System.out.println("ADD RESULTING DAMAGE PICTURE");
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View damagePictureSet = layoutInflater.from(this).inflate(R.layout.damage_picture_set, picturesCell, false);
((TextView) damagePictureSet.findViewById(R.id.removePicture)).setTypeface(fontAwesome);
TextView remove = (TextView)damagePictureSet.findViewById(R.id.removePicture);
remove.setTypeface(fontAwesome);
File imgFile = new File(pathToFile);
if(imgFile.exists()) {
System.out.println("ADD RESULTING DAMAGE PICTURE - FILE OK");
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 4;
final Bitmap thebmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), opt);
((ImageView) damagePictureSet.findViewById(R.id.damagePicture)).setImageBitmap(thebmp);
picturesCell.addView(damagePictureSet);
} else {
System.out.println("ADD RESULTING DAMAGE PICTURE - NO FILE EXISTS!!!");
}
return;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SHOOT_DAMAGE_PICTURE && resultCode == RESULT_OK) {
final String encodedPicture = data.getStringExtra("PATH_TO_PICTURE");
System.out.println("PICTURE RESULT = " + encodedPicture);
addResultingDamagePicture(encodedPicture);
} else {
System.out.println("ON ACTIVITY RESULT = NOT OK");
return;
}
}
在某些情况下,onCreate
方法可以在 onActivityResult
方法之后调用。在这种情况下,setContentView
方法会再次调用,再次渲染初始布局。您可能想要设置一些标志,或者独立于 Activity
更新数据,以便您可以根据标志/数据重新创建 View
。
我正在尝试以编程方式添加特定的相对布局 xml。它由一个 ImageView、EditText 和一个 TextView 组成。问题似乎是我试图在从另一个 activity 返回后在 "onActivityResult" 方法上执行此操作。当我在其他地方这样做时它可以工作并且我想创建的布局正确显示(例如我成功尝试 "onCreate" 并按下按钮),但如果在 "onActivityResult" 内则不起作用。我需要它在那里。
这两种情况打印都输出成功,没有报错和崩溃,就是排版不显示。任何建议表示赞赏。
这是我要创建的 xml 文件:
<?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="260dip">
<ImageView
android:id="@+id/damagePicture"
android:layout_width="match_parent"
android:layout_height="210dip"
android:scaleType="centerCrop"
android:layout_marginTop="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:src="@drawable/nocar"/>
<EditText
android:id="@+id/orderTextfield"
android:layout_height="40dip"
android:layout_width="match_parent"
android:gravity="left|center"
android:layout_marginLeft="10dip"
android:layout_marginRight="45dip"
android:hint="Enter damage description"
android:textSize="14sp"
android:layout_alignParentLeft="true"
android:textColor="@color/black"
android:textColorHint="@color/gray_vin_search_text"
android:background="@null"
android:singleLine="true"
android:editable="true"
android:layout_below="@+id/damagePicture"/>
<TextView
android:id="@+id/removePicture"
android:layout_width="40dip"
android:layout_height="40dip"
android:text="@string/discardPicture"
android:gravity="center|center"
android:textColor="@color/black"
android:textSize="24sp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:clickable="true"
android:onClick="removePicture"/>
<View
android:id="@+id/bottomLine"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_alignParentBottom="true"
android:background="@color/gray_for_line"></View>
</RelativeLayout>
这里是代码的相关部分:
public void addResultingDamagePicture(String pathToFile) {
System.out.println("ADD RESULTING DAMAGE PICTURE");
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View damagePictureSet = layoutInflater.from(this).inflate(R.layout.damage_picture_set, picturesCell, false);
((TextView) damagePictureSet.findViewById(R.id.removePicture)).setTypeface(fontAwesome);
TextView remove = (TextView)damagePictureSet.findViewById(R.id.removePicture);
remove.setTypeface(fontAwesome);
File imgFile = new File(pathToFile);
if(imgFile.exists()) {
System.out.println("ADD RESULTING DAMAGE PICTURE - FILE OK");
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 4;
final Bitmap thebmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), opt);
((ImageView) damagePictureSet.findViewById(R.id.damagePicture)).setImageBitmap(thebmp);
picturesCell.addView(damagePictureSet);
} else {
System.out.println("ADD RESULTING DAMAGE PICTURE - NO FILE EXISTS!!!");
}
return;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SHOOT_DAMAGE_PICTURE && resultCode == RESULT_OK) {
final String encodedPicture = data.getStringExtra("PATH_TO_PICTURE");
System.out.println("PICTURE RESULT = " + encodedPicture);
addResultingDamagePicture(encodedPicture);
} else {
System.out.println("ON ACTIVITY RESULT = NOT OK");
return;
}
}
在某些情况下,onCreate
方法可以在 onActivityResult
方法之后调用。在这种情况下,setContentView
方法会再次调用,再次渲染初始布局。您可能想要设置一些标志,或者独立于 Activity
更新数据,以便您可以根据标志/数据重新创建 View
。