本文介绍了为什么我的layout_weight不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个xml文件.我不明白为什么我第二个包含editText和callButton的linearlayout看起来不正确.看起来像这样(与电话的局部图片一致):

I have this xml file.I can't understand why my second linearlayout which contains editText and callButton isn't looking right. It looks like this (the line with the part picture of phone) :

在每个视图(editText和callButton)中,我都将layout_weights设置为1,所以电话图像应该占据屏幕的一半,对不对?这是我的代码.我希望两个视图都采用相同的宽度-输入文本时,editText不应拉伸,这是目前正在发生的情况.感谢您的帮助.

I have the layout_weights set to 1 in each of these views, editText and callButton, so the phone image should take up half the screen, right? Here's my code. I would like both views to take up the same width - and the editText shouldn't stretch when text is entered, as is currently happening. Thanks for any help.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/ListView_2"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="1">
    </ListView>

<LinearLayout
    android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        >
        <!--android:adjustViewBounds="true"-->
    <!-- we want the text to begin in the centre of the edittext view
    that's what gravity does-->

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/editText"
        android:inputType="phone"
        android:gravity="center"
        android:background="#d9d9d9"
        android:layout_weight="1"

        />


        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/callButton"
            android:src="@drawable/call"
            android:clickable="true"
            android:onClick="softkeyboardButton"
            android:background="#ffa62b"
            android:layout_weight="1"

            />
</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal" >

    <CheckBox
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:background="#ffa500"
        android:text="New CheckBox"
        android:id="@+id/checkBox"
        android:layout_weight="4"
        />


        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:id="@+id/imageButton"
            android:src="@drawable/softkeyboard"
            android:clickable="true"
            android:onClick="softkeyboardButton"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:background="@drawable/buttonstates"
             />

        <View android:layout_height="fill_parent"
            android:layout_width="2px"
            android:background="#90909090"/>

        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:id="@+id/imageButton2"
            android:src="@drawable/settingsicon"
            android:background="@drawable/buttonstates"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1" />

    </LinearLayout>

    </LinearLayout>

推荐答案

将具有layout_width的EditText和Call按钮更改为"0dp"-

Change your EditText and Call button with layout_width to "0dp" -

<EditText
        android:layout_height="match_parent"
        android:id="@+id/editText"
        android:inputType="phone"
        android:gravity="center"
        android:background="#d9d9d9"
        android:layout_weight="1"

        />


        <ImageButton
            android:layout_height="match_parent"
            android:id="@+id/callButton"
            android:src="@drawable/call"
            android:clickable="true"
            android:onClick="softkeyboardButton"
            android:background="#ffa62b"
            android:layout_weight="1"

            />

这篇关于为什么我的layout_weight不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:54