【问题标题】:How can you make your android application occupy only half of the screen?怎样才能让你的android应用只占屏幕的一半呢?
【发布时间】:2014-05-26 03:19:08
【问题描述】:

我有一个列表,但它会占据平板电脑的所有屏幕。有没有办法让它只占平板电脑屏幕的一半或1/4?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" />

</LinearLayout>

【问题讨论】:

    标签: android android-layout layout screen-size


    【解决方案1】:

    添加另一个空白视图或要放置在另一半的视图。将其布局权重设置为 0.5:android:layout_weight=".5"

    android:layout_weight=".5" 设置为ListView

    请注意,将 layout_weight 设置为 0.5,如果容器中有单个视图将无效。

    祝你好运!

    【讨论】:

    • 我的意思是你的应用程序只占据了屏幕的一半。因此,您可以看到其他应用程序图标。所以如果有一个应用程序只能占据一半的屏幕,你可以同时运行。
    【解决方案2】:

    您可以在 XML 中使用以下内容进行 垂直 屏幕分割: 这里我们使用 weightsum 和 weight 属性。 您可以调整权重属性以获得所需的一半或四分之一等结果。

    <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="match_parent"
    android:orientation="vertical"
    android:weightSum="100" >
    
      <RelativeLayout
         android:id="@+id/button1"
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="50"
         android:background="@android:color/black" >
    
         <!-- Your RelativeLayout Items -->
    
      </RelativeLayout>
    
    </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="match_parent"
      android:background="@android:color/darker_gray"
      android:orientation="horizontal"
      android:weightSum="100" >
    
      <RelativeLayout
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:background="@android:color/black" >
    
          <!-- Your RelativeLayout Items -->
    
      </RelativeLayout>
    
    </LinearLayout>
    

    编辑: 要显示为小部件,您可以参考这里的教程http://www.vogella.com/tutorials/AndroidWidgets/article.html 和这里http://www.tutorialspoint.com/android/android_widgets.htm

    【讨论】:

    • 请看我对另一个答案的看法。
    • @user3516943:为此,您只能将应用程序创建为小部件。类似于手机中通常可用的天气小部件等。可以参考编辑
    • 好的,所以答案是小部件作为显示器。酷!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    相关资源
    最近更新 更多