列表占用太多空间,无法查看带按钮的 EditText

发布于 2025-01-03 11:30:08 字数 1693 浏览 1 评论 0原文

我想要一个 ListView,后跟一个 EdiText,然后是 2 个按钮,例如:

ListView <NEWLINE>
EditText  <NEWLINE>
Button1 Button2 <NEWLINE>

我有代码,如果列表很小,一切都会顺利。但是,如果列表有很多元素,它将占用所有空间,并且我将无法看到 EditText 或按钮。

我已将其精简到问题开始的部分:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView android:id="@+id/in"
    android:layout_below="@id/button_contacts"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_weight="1.0" 
    />
    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/in" />

   <Button
        android:id="@+id/Convert"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="Convert" />
   <Button
    android:id="@+id/sendSMS"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@id/Convert"
        android:layout_alignTop="@id/Convert"
        android:text="Send" />


</RelativeLayout>

我该如何解决这个问题?

I want to have a ListView, followed by a EdiText, folowed by 2 buttons like:

ListView <NEWLINE>
EditText  <NEWLINE>
Button1 Button2 <NEWLINE>

I have the code and if the list is small everything goes well. However, if the list has many elements it will take all the space and I won't be able to see the EditText or the buttons.

I have trimmed it down to the part where the problem starts:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView android:id="@+id/in"
    android:layout_below="@id/button_contacts"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_weight="1.0" 
    />
    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/in" />

   <Button
        android:id="@+id/Convert"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="Convert" />
   <Button
    android:id="@+id/sendSMS"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@id/Convert"
        android:layout_alignTop="@id/Convert"
        android:text="Send" />


</RelativeLayout>

How can I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橙味迷妹 2025-01-10 11:30:08

下面的代码对我有用,我希望它也对你有用......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              style="@style/MainLinearLayout">"

<TableLayout    android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" 
                style="@style/MainTableLayoutStyle">

    <ListView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@android:id/list"
              android:layout_weight="1"
              android:cacheColorHint="@color/transparent" 

              >

    </ListView>

    <TableRow>
       <EditText
            android:id="@+id/entry"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:drawable/editbox_background"
            android:layout_below="@id/in" />

    </TableRow>

    <TableRow >

    <Button  android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:id="@+id/button1"
                 android:text="button1"
                 android:layout_gravity="left"
                 android:layout_weight="1"
                 />

    <Button  android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:id="@+id/button1"
                 android:text="button2"
                 android:layout_gravity="right"
                 android:layout_weight="1"
                 />


    </TableRow>

</TableLayout>

</LinearLayout>

the below code works for me and i hope it will work for you too...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              style="@style/MainLinearLayout">"

<TableLayout    android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" 
                style="@style/MainTableLayoutStyle">

    <ListView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@android:id/list"
              android:layout_weight="1"
              android:cacheColorHint="@color/transparent" 

              >

    </ListView>

    <TableRow>
       <EditText
            android:id="@+id/entry"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:drawable/editbox_background"
            android:layout_below="@id/in" />

    </TableRow>

    <TableRow >

    <Button  android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:id="@+id/button1"
                 android:text="button1"
                 android:layout_gravity="left"
                 android:layout_weight="1"
                 />

    <Button  android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:id="@+id/button1"
                 android:text="button2"
                 android:layout_gravity="right"
                 android:layout_weight="1"
                 />


    </TableRow>

</TableLayout>

</LinearLayout>
时光倒影 2025-01-10 11:30:08

因为在 ListView 中你使用 android:layout_weight="1.0" 所以,它占用了所有空间。您应该添加 1 个布局来包装您的 Text、Buttom 等,并添加此 android:layout_weight="2.0" 或 3.0(由您决定)尝试。

PS如果仍然不起作用尝试将所有 android:layout_height="fill_parent" 更改为 android:layout_height="wrap_content"

    <ListView android:id="@+id/in"
        android:layout_below="@id/button_contacts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1.0" 
        />
<Relativelaout
        android:id="@+id/bottomcontrol"
        android:layout_below="@id/in"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="2.0">
<EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:drawable/editbox_background"
         />

   <Button
        android:id="@+id/Convert"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="Convert" />
   <Button
    android:id="@+id/sendSMS"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@id/Convert"
        android:layout_alignTop="@id/Convert"
        android:text="Send" />
</RelativeLayout>

Because in ListView you use android:layout_weight="1.0" so, it took all space. you should add 1 layout to wrap your Text,Buttom etc. and add this android:layout_weight="2.0" or 3.0 (it's up to you) Try.

P.S. If it still not work For Try change all android:layout_height="fill_parent" To android:layout_height="wrap_content"

    <ListView android:id="@+id/in"
        android:layout_below="@id/button_contacts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1.0" 
        />
<Relativelaout
        android:id="@+id/bottomcontrol"
        android:layout_below="@id/in"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="2.0">
<EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:drawable/editbox_background"
         />

   <Button
        android:id="@+id/Convert"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="Convert" />
   <Button
    android:id="@+id/sendSMS"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@id/Convert"
        android:layout_alignTop="@id/Convert"
        android:text="Send" />
</RelativeLayout>
郁金香雨 2025-01-10 11:30:08

您需要给列表视图一个固定的高度。

 <ListView android:id="@+id/in"
    android:layout_below="@id/button_contacts"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_weight="1.0" 
    />

另一种选择是在代码中以编程方式设置高度。如果您这样做,您需要在 Activity 的 onWindowFocused() 方法中执行。然后,您可以通过编程方式将其设置为屏幕的一半。

Private LinearLayout llfullscreen;//llfullscreen is the viewgroup 
                                  //that stretches the whole screen
Private ListView lv;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        llfullscreen = findViewById(R.id.bottomcontrol)
}
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
            if(hasFocus && llfullscreen!=null){
        int layoutHeight = llfullscreen.getHeight(); 
                ViewGroup.LayoutParams params = lv.getLayoutParams();
        params.height = lv/2; //Set to half the screens size
        this.lv.setLayoutParams(params);
        this.lv.invalidate();
             }
    }

You need to give your listview a fixed height.

 <ListView android:id="@+id/in"
    android:layout_below="@id/button_contacts"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_weight="1.0" 
    />

Another choice is to set the height programmatically in the code. If you do this you need to do in the Activity's onWindowFocused() method. You can then programmatically set it to say half your screen.

Private LinearLayout llfullscreen;//llfullscreen is the viewgroup 
                                  //that stretches the whole screen
Private ListView lv;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        llfullscreen = findViewById(R.id.bottomcontrol)
}
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
            if(hasFocus && llfullscreen!=null){
        int layoutHeight = llfullscreen.getHeight(); 
                ViewGroup.LayoutParams params = lv.getLayoutParams();
        params.height = lv/2; //Set to half the screens size
        this.lv.setLayoutParams(params);
        this.lv.invalidate();
             }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文