我已经在活动中完成了这项工作,我也将提供部分代码。基本上我在HorizontalScroll View中有一个线性布局,我想动态地添加imageViews。这个活动代码在下面,它完全正常,我想要的是我的1个片段也具有完全相同的功能。代码的XML如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
tools:context="com.example.scrolls_tabs.OneFragment"
android:background="@drawable/wooden_back">
<HorizontalScrollView
android:id="@+id/HorizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#00ffffff"
android:scrollbars="none">
<LinearLayout
android:id="@+id/MainLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/imgLayout1"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
以及在活动中完美运作的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout =(RelativeLayout)findViewById(R.id.activity_main);
layout.setBackgroundResource(R.drawable.wooden_back);
// Bitmap bdrawable = new Bitmap(this.getResources().getIdentifier("drawable/wood_back", "id", getPackageName()));
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenHeight = displaymetrics.heightPixels;
int screenWidth = displaymetrics.widthPixels;
int imgHeight = (int) (screenHeight* 0.30);
int imgWidth = (int) (screenWidth* 0.25);
for (int j=1; j<10; j++)
{
final int b1=j;
create_img1("drawable/a"+j, b1, imgHeight, imgWidth);
ImageView drag = (ImageView)findViewById(b1);
dragAndDropImage(drag, "drawable/c"+j);
}
ImageView imgh=(ImageView)findViewById(R.id.bottomlinear);
imgh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle localBundle = new Bundle();
localBundle.putInt("Item #1", item1);
localBundle.putInt("Item #2", item2);
localBundle.putInt("Item #3", item3);
localBundle.putInt("Item #4", item4);
localBundle.putInt("Item #5", item5);
localBundle.putInt("Item #6", item6);
localBundle.putInt("Item #7", item7);
localBundle.putInt("Item #8", item8);
localBundle.putInt("Item #9", item9);
Intent i = new Intent(getApplicationContext(),test_activity.class);
i.putExtras(localBundle);
startActivityForResult(i, 1);
}
});
}
void create_img1(String ss, int ID, int H, int W)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout1);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 0, 20, 0);
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(parms);
int id = getResources().getIdentifier(ss, "id", getPackageName());
imageView.setImageResource(id);
linearLayout.addView(imageView);
imageView.setId(ID);
imageView.getLayoutParams().height = H;
imageView.getLayoutParams().width = W;
}
这基本上是我在寻找一个解决方案,我可以在片段中做同样的事情,我已经在活动中完成了。我对android很新,所以任何有用的东西都会受到赞赏。
当前片段代码无效。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.fragment_one, container, false);
int imgHeight = 50;
int imgWidth = 50;
for (int j=1; j<10; j++)
{
final int b1=j;
create_img1("drawable/a"+j, b1, imgHeight, imgWidth);
}
return ll;
}
void create_img1(String ss, int ID, int H, int W)
{
LinearLayout linearLayout = (LinearLayout) ll.findViewById(R.id.imgLayout1);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 0, 20, 0);
final ImageView imageView = new ImageView(getActivity());
imageView.setLayoutParams(parms);
//int id = getResources().getIdentifier(ss, "id", getPackageName());
imageView.setImageResource(R.drawable.a1);
linearLayout.addView(imageView);
imageView.setId(ID);
//imageView.getLayoutParams().height = H;
//imageView.getLayoutParams().width = W;
}
}
答案 0 :(得分:0)
你应该将img视图添加到线性布局,如下所示
void create_img1(String ss, int ID, int H, int W)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout1);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 0, 20, 0);
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(parms);
int id = getResources().getIdentifier(ss, "id", getPackageName());
imageView.setImageResource(id);
imageView.setId(ID);
imageView.getLayoutParams().height = H;
imageView.getLayoutParams().width = W;
**linearLayout.addView(imageView);**
}