我想在ImageView
上添加具有特定LinearLayout
的{{1}}。
当我使用LayoutParams
中定义的ImageView
时,dot_layout.xml
工作正常(注释行)。但是,当我动态创建并添加layoutParams
布局参数(在for循环中)时,ImageView
不起作用。
leftMargin
这是我的dot_layout.xml:
public void addDots() {
LinearLayout dotLayout = (LinearLayout) findViewById(R.id.dot_layout);
dotLayout.removeAllViewsInLayout();
//ImageView dotImage = (ImageView) findViewById(R.id.slider_dot);
//LayoutParams layoutParams = (LayoutParams) dotImage.getLayoutParams();
//layoutParams.leftMargin = 100;
//dotImage.setLayoutParams(layoutParams);
for(int i=0; i<INTERVAL; i++) {
ImageView dot = new ImageView(mContext);
dot.setImageDrawable(getResources().getDrawable(R.drawable.slider_dot));
LayoutParams layoutParams = (LayoutParams) new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = (int) ((i*mLengthOfSlider/INTERVAL)*mDensity);
dotLayout.addView(dot, layoutParams);
}
}
我期待点图片的视图如:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/dot_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="horizontal" />
<ImageView
android:id="@+id/slider_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/slider_dot"
/>
</RelativeLayout>
但是我得到了:* * * *
(没有左边距)
我在这里做错了什么?任何人都可以帮助我吗?
答案 0 :(得分:1)
试试这个
LinearLayout ll = (LinearLayout)findViewById(R.id.layout);
并添加一个ImageButton
ImageButton myButton = new ImageButton(this);
myButton.setBackgroundDrawable(demoCoverImage);
LayoutParams lp = new LayoutParams(70, 60);
ll.addView(myButton, lp);