我有個親戚佈局塔具有以下XML:編程方式將項目添加到相對佈局
<RelativeLayout
android:id="@+id/date_bar_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/navigation_bar_layout"
android:background="@color/light_gray"
android:gravity="center">
<TextView
android:id="@+id/detaildatetextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text=""
android:textColor="@color/black"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
正如你可以看到它已經在這一個TextView。在我的代碼中的某處,我需要在文本視圖之前以編程方式添加圖像。這意味着首先將顯示圖像,然後顯示文本視圖。但是當我執行代碼和文本視圖時,圖像會顯示。請幫我解決這個問題。我的實施如下:
image = new ImageView(getApplicationContext());
image.setId(0);
dateText = (TextView) dateBarLayout.findViewById(R.id.detaildatetextview);
dateText.setText("Dummy text");
RelativeLayout.LayoutParams params = (LayoutParams) dateBarLayout.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, dateText.getId());
String icon = "drawable/"+ path;
icon = icon.replaceAll("-", "_");
icon = icon.toLowerCase();
int imageResource = this.getApplicationContext().getResources().getIdentifier(icon, null,this.getApplicationContext().getPackageName());
image.setImageResource(imageResource);
dateBarLayout.addView(image);
我不知道我要去哪裏錯了..請幫助我擺脫這一點。在此先感謝...-) :-)
你要顯示的圖像文本的圖像和頂部? –
不,我希望首先顯示圖像,然後用簡單的文字從左到右的圖像顯示,然後顯示文本。 –
@UsamaSarwar請看我的答案,它會解決你的問題。 –