簡體   English   中英

如何在LinearLayout內的單行中設置ImageView和textView

[英]How to set ImageView and the textView in a single line inside a LinearLayout

我正在努力在LinearLayout內的單行中設置imageView和textView。 我嘗試了不同的方法,但仍然找不到合適的解決方案。 這是我使用的代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:clickable="true"
    android:orientation="vertical"
    android:paddingLeft="40dp"
    tools:context=".MainActivity" >

    <ImageView 
        android:id="@+id/icon_image"
        android:layout_width="30sp"
        android:layout_height="30sp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:layout_gravity="bottom"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="5dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:textSize="14sp"
        android:textStyle="bold" >
    </TextView>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />

</LinearLayout> 

這就是它的出現方式: 在此輸入圖像描述

我該怎么做才能做到這一點?

它位於具有垂直orientationLinearLayout內部,因此當然所有東西都是“垂直”堆疊的。 將它們包裹在具有水平orientationLinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >
<LinearLayout
    .../>
<ImageView 
    android:id="@+id/icon_image"
    android:layout_width="30sp"
    android:layout_height="30sp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:layout_gravity="bottom"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher"/>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawablePadding="5dp"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:textSize="14sp"
    android:textStyle="bold" >
</TextView>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/black" />

</LinearLayout> 

默認情況下, LinearLayout具有水平orientation因此無需在包含兩個View的子LinearLayout提供該屬性。

Use android:layout_gravity="center_vertical" for your textview

我建議使用RelativeLayout。 這樣你也可以垂直添加更多項目。

您需要更改linearylayout的方向。 只需更改android:orientation="vertical"

android:orientation="horizontal"

您可以嘗試在TableLayout中包含ImageViews和TextViews

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >

<TableLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TableRow>

<ImageView 
    android:id="@+id/icon_image"
    android:layout_width="30sp"
    android:layout_height="30sp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:layout_gravity="bottom"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher"/>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawablePadding="5dp"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:textSize="14sp"
    android:textStyle="bold" />
</TableRow>
</TableLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/black" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM