我正在设置第一个RelativeLayout
的左边距,并且
我有这个xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/market_layout"
android:orientation="horizontal">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/horizontalScrollView"
android:layout_gravity="center_horizontal"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/market_linear_layout_in_hsv">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
在LinearLayout
中的{java}代码插入id = market_linear_layout_in_hsv
插入RelativeLayout
,包括一些视图(Button, TextView, ImageView)
:
market_ll = (LinearLayout)findViewById(R.id.market_linear_layout_in_hsv);
RelativeLayout.LayoutParams TVParams = new RelativeLayout.LayoutParams(RLwrapContent, RLwrapContent );
RelativeLayout.LayoutParams IVParams = new RelativeLayout.LayoutParams(250, 400);
RelativeLayout.LayoutParams BParams = new RelativeLayout.LayoutParams(RLwrapContent, RLwrapContent);
RelativeLayout.LayoutParams RLParams = new RelativeLayout.LayoutParams(RLMatchParent, RLMatchParent);
TVParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
TVParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
IVParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
IVParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
BParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
BParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
if (folder.exists()) {
String[] files = folder.list();
relationsIdMarket = new ArrayList<RelationsIdMarket>(files.length);
for (int i = 0; i < files.length; i++){
RLParams.setMargins(i == 0?150:0, 0, 0, 0);//here i set on first iteration left margin 150
RelationsIdMarket rim = new RelationsIdMarket();
rim.idProduct = i;
rim.idTextView = ViewIdGenerator.generateViewId();
rim.idImageView = ViewIdGenerator.generateViewId();
rim.idButton = ViewIdGenerator.generateViewId();
TextView tv = new TextView(this);
tv.setText("name");
tv.setId(rim.idTextView);
ImageView iv = new ImageView(this);
if(GeneralData.images.size() == i)
GeneralData.images.add(BitmapFactory.decodeFile(pathToImg+"/"+files[i]));
iv.setImageBitmap(GeneralData.images.get(i));
iv.setId(rim.idImageView);
iv.setImageBitmap(rp.bmp);
Button btn = new Button(this);
btn.setText("open");
btn.setOnClickListener(this);
btn.setId(rim.idButton);
RelativeLayout rl = new RelativeLayout(this);
rl.addView(tv, TVParams);
rl.addView(iv, IVParams);
rl.addView(btn, BParams);
rl.setPadding(50, 0, 50, 0);
market_ll.addView(rl, RLParams);
relationsIdMarket.add(rim);
}
}
但我还没有能够实现这一目标。任何帮助表示赞赏。 我纠正了我的代码如下:
ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(RLMatchParent, RLMatchParent);
if (folder.exists()) {
String[] files = folder.list();
relationsIdMarket = new ArrayList<RelationsIdMarket>(files.length);
for (int i = 0; i < files.length; i++){
mlp.leftMargin = i==0?100:0;
RelationsIdMarket rim = new RelationsIdMarket();
rim.idProduct = i;
rim.idTextView = ViewIdGenerator.generateViewId();
rim.idImageView = ViewIdGenerator.generateViewId();
rim.idButton = ViewIdGenerator.generateViewId();
TextView tv = new TextView(this);
tv.setText("Название");
tv.setId(rim.idTextView);
ImageView iv = new ImageView(this);
if(GeneralData.images.size() == i)
GeneralData.images.add(BitmapFactory.decodeFile(pathToImg+"/"+files[i]));
iv.setImageBitmap(GeneralData.images.get(i));
iv.setId(rim.idImageView);
iv.setImageBitmap(rp.bmp);
Button btn = new Button(this);
btn.setText("Открыть");
btn.setOnClickListener(this);
btn.setId(rim.idButton);
RelativeLayout rl = new RelativeLayout(this);
rl.addView(tv, TVParams);
rl.addView(iv, IVParams);
rl.addView(btn, BParams);
rl.setPadding(50, 0, 50, 0);
market_ll.addView(rl, mlp);
relationsIdMarket.add(rim);
}
}
但问题没有解决。
答案 0 :(得分:-1)
在第一个线性布局中使用android:layout_marginLeft
Android RelativeLayout
继承自android.view.ViewGroup.MarginLayoutParams
,它提供了为布局设置边距的方法
参考Source