如何将textview放在listview中的imageview下面?

时间:2015-01-30 07:02:43

标签: android xml listview textview imageview

我想将我的textview放在listview中的imageview下,当我尝试使用相对时它会给出一些错误,所以我使用了linearlayout但我不能把textview放在imageview下你能不能帮我谢谢你提前吗?

xml listview:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bacground"
android:orientation="horizontal" >

<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1" />

</LinearLayout>
listview_layout中的

xml:

     <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id="@+id/LinearLayout1"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<ImageView
    android:id="@+id/flag"
    android:layout_width="400dp"
    android:layout_height="300dp"
    android:contentDescription="@string/hello"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
   />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center" >

<Textview
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@color/white"
        android:layout_marginTop="90dp"
        android:layout_marginRight="1dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:textSize="20dp" />
</LinearLayout>

我的代码:

        OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View container,
                int position, long id) {

            Intent b_intent = new Intent(getApplicationContext(),
                    PicSelectActivity.class);
            startActivity(b_intent);

            // Getting the Container Layout of the ListView
            LinearLayout linearLayoutParent = (LinearLayout) container;
            // Getting the inner Linear Layout
            LinearLayout linearLayoutChild = (LinearLayout) linearLayoutParent
                    .getChildAt(1);
            // Getting the Country TextView
            TextView tvCountry = (TextView) linearLayoutChild.getChildAt(0);

            Editor editor = sharedpreferences.edit();
            editor.putString("getFrame", tvCountry.getText().toString());
            editor.commit();

        }
    };
    // Setting the item click listener for the listview
    listView.setOnItemClickListener(itemClickListener);

6 个答案:

答案 0 :(得分:1)

只做你的根布局

android:orientation="vertical"

答案 1 :(得分:0)

根线性布局方向更改为垂直,并删除其他不必要的线性布局

答案 2 :(得分:0)

上面你写了一个很长的xml,但你写下面的代码并实现你的目标。使用文字视图并为图片设置android:drawableTop,为文字设置android:text

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id="@+id/LinearLayout1"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_launcher"
        android:text="TextView" />
</LinearLayout>

答案 3 :(得分:0)

***检查这个* **

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/race_entrant"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#E0EAF1"
    android:descendantFocusability="blocksDescendants"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingBottom="2dip"
    android:paddingRight="2dip"
    android:paddingTop="2dip" >

<LinearLayout
    android:id="@+id/linearLeft"
    android:layout_width="50dip"
    android:layout_height="fill_parent"
    android:background="#E0EAF1"
    android:cacheColorHint="#E0EAF1"
    android:orientation="vertical"
    android:paddingBottom="2dip"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:paddingTop="2dip" >

    <ImageView
        android:src="@drawable/ic_launcher"
        android:layout_weight="1"
        android:id="@+id/imgJockey"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="1dip"
        android:adjustViewBounds="true"
        android:cacheColorHint="#E0EAF1" />

    <TextView
        android:text="Text"
        android:id="@+id/txtEntrantNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/entrant_number"
        android:layout_gravity="center_horizontal"
        android:cacheColorHint="#E0EAF1" />

    </LinearLayout>

</LinearLayout>

答案 4 :(得分:0)

你不能使用这个

    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
  android:layout_alignParentLeft="true"

因为这是RelativeLayout而不是LinearLayout的一部分。

下面的代码将解决您的问题。试试这个。

 <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/flag"
            android:layout_width="400dp"
            android:layout_height="300dp"
            android:contentDescription="@string/hello" />


            <Textview
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:textSize="20dp" />
        </LinearLayout>

答案 5 :(得分:0)

1。使用android:orientation="vertical&#34;到你的根LinearLayout

2。删除子LinearLayout并更新TextViewImageView属性,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/flag"
        android:layout_width="400dp"
        android:layout_height="300dp"
        android:src="@drawable/dummy"/>

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#ff0000"
        android:layout_marginTop="8dp"
        android:layout_marginRight="1dp"
        android:textSize="20dp"
        android:text="This is a sample text." />

</LinearLayout>

3。onItemClick()方法中试试这个:

    @Override
    public void onItemClick(AdapterView<?> parent, View container,
            int position, long id) {

        Intent b_intent = new Intent(getApplicationContext(),
                PicSelectActivity.class);
        startActivity(b_intent);

        // Getting the Container Layout of the ListView
        LinearLayout linearLayoutParent = (LinearLayout) container;

        // Getting the Country TextView
        TextView tvCountry = (TextView) linearLayoutParent.getChildAt(1);

        Editor editor = sharedpreferences.edit();
        editor.putString("getFrame", tvCountry.getText().toString());
        editor.commit();

    }

<强>输出:

enter image description here

希望这会有所帮助〜