xml file --->>>>
<HorizontalScrollView
android:id="@+id/dashboard_hs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:fillViewport="false"
android:scrollbarSize="3dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rlforImgGallery">
<LinearLayout
android:id="@+id/imageGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:paddingTop="50dp">
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view_name"
android:gravity="center"
android:layout_below="@+id/imageGallery"
android:orientation="horizontal"
android:layout_marginTop="5dp">
</LinearLayout>
</RelativeLayout>
</HorizontalScrollView>
Java文件----&gt;&gt;&gt;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER_VERTICAL;
//RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
// ViewGroup.LayoutParams.WRAP_CONTENT);
// rp.addRule(RelativeLayout.BELOW, R.id.imageGallery);
lp.setMargins(0, 0, 60, 0);
lp1.setMargins(60,0,150,0);
lp1.gravity = Gravity.CENTER_VERTICAL;
// tv.setLayoutParams(rp);
tv.setTextColor(Color.parseColor("#000000"));
imageGallery.addView(imgView);
//imageGallery.addView(tv);
name_view.addView(tv);
//relativeLayout.addView(tv);
imgView.setLayoutParams(lp);
tv.setLayoutParams(lp1);
我在HorizontalScrollView中有两个lineat布局,一个LinearLayout动态显示图像,另一个LinearLayout显示文本(即用户名)。问题是,它没有在相应的图像下显示文字。
答案 0 :(得分:0)
由于您对WRAP_CONTENT
和ImageView
使用了TextView
,因此他们无法对齐。布局中另一个令人不安的问题是使用<RelativeLayout>
(这里的性能问题)可以避免。我建议你制作如下的父布局:
<HorizontalScrollView
android:id="@+id/dashboard_hs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:fillViewport="false"
android:scrollbarSize="3dp">
<!-- Will add views here -->
</HorizontalScrollView>
现在,您可以定义一个固定的&#39;布局将保存您的每个用户数据。定义它是这样的(模糊的例子):
// cell_layout.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:layout_width="160dp"
android:layout_height="160dp"
android:padding="20dp"
android:id="@+id/imgView"
android:src="@android:color/holo_red_dark"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:text="Funk you :)"
android:layout_margin="20dp"/>
</LinearLayout>
因为,我们自己定义了布局,我们保证有一个明确的安排。我们现在可以在运行时扩充这个布局:
ScrollView scrollView = (ScrollView) findViewById(R.id.dashboard_hs);
LayoutInflater inflater = getLayoutInflater();
// -----
// Following segment to be repeated for each of your users
LinearLayout ll_main = (LinearLayout) inflater.inflate(R.layout.cell_layout, scrollView, false);
ImageView imgView = (ImageView) ll_main.findViewByIf(R.id.imgView);
imgView.setImageResource(R.id.user);
TextView textView = (TextView) ll_main.findViewById(R.id.textView);
textView.setText("UserName");
scrollView.addView(ll_main);
// -----
完成!如果你有任何困惑,请告诉我。