Android 如何在LinearLayout内的单行中设置ImageView和textView

Android 如何在LinearLayout内的单行中设置ImageView和textView,android,android-layout,Android,Android Layout,我正在努力在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"

我正在努力在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> 

这是它的外观:


我应该怎么做才能纠正这个问题呢?

它是一个垂直
方向的
线性布局的内部
,因此自然所有东西都是“垂直”堆放的。以水平方向将其包装成
线性布局

<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
具有水平
方向
,因此无需在封装两个
视图的子
LinearLayout
中提供该属性。

我建议使用RelativeLayout。这样,您也可以垂直添加更多项目

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


您可以尝试将ImageView和TextView包含在TableLayout中

<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" />


谢谢你的回答。但是仅仅将当前方向更改为水平方向是不起作用的。谢谢您的建议,但无法更改布局。我需要在代码的其余部分使用LinearLayout。谢谢您的回答。我照你说的做了。但还是有一个小问题。文本不显示在行中。它涨了一点。我应该怎么做来纠正它?不确定,因为我不知道“它有点高”到底是什么。也许你想使用android:layout\u gravity=“bottom”
,因为这就是你的
ImageView
的功能。啊,是的,我试过用android:gravity=“bottom”来实现这两个功能。虽然图像位于行上,但文本一直显示到行上。然后在您的
文本视图中设置
android:gravity=“bottom”
,我已经在前面的注释中这样做了:)。仍然存在同样的问题(感谢您的支持。但我需要在代码的其余部分使用:)TableLayout将被视为布局中的布局。如果有帮助,您可以将android:layout\u gravity=“center\u vertical”添加到文本视图中,使其居中
<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" />
Use android:layout_gravity="center_vertical" for your textview