【问题标题】:How do I set any layout(width,height) for tv?如何为电视设置任何布局(宽度、高度)?
【发布时间】:2012-03-21 20:24:38
【问题描述】:

我正在创建一个在罗技 revue 上运行的应用程序。用户界面显示在电视屏幕上。

使用模拟器运行时布局看起来不错。但是当它显示在电视屏幕上时,部分内容会被截断。

如何调整:

  • android:layout_width
  • android:layout_height

无论大小,它都适合屏幕吗? fill-parent 和 wrap_content 似乎没有解决这个问题。

如果可能的话,您能否指导我获取一些资源或提供示例?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <GridView
        android:id="@+id/propGridView"
        android:layout_width="619dp"
        android:layout_height="487dp"
        android:layout_gravity="center_vertical"
        android:numColumns="5"
        android:paddingTop="20dp" >
    </GridView>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="380dp"
        android:layout_height="508dp"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@+id/frameLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >

            <ImageView
                android:id="@+id/propertyThumbnail"
                android:layout_width="match_parent"
                android:layout_height="280dp"
                android:layout_weight="0.10"
                android:paddingTop="20dp"
                android:src="@drawable/wow" />
        </FrameLayout>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="5dp"
            android:text="Title"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Description"
            android:textSize="10dp" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 为您的布局发布 XML,以便我们查看所有内容
  • 看看我的回答,看看有没有帮助。

标签: android android-layout layout


【解决方案1】:

设置固定值可能会导致您的视图出现问题。前任。如果您有一个宽度为 200dip 的视图,另一个为 400dip 的视图,并且屏幕支持的宽度为 500dip,那么您的视图中的内容将被截断。

考虑为您的宽度使用权重。如果您为一个视图提供 0.6 的权重,而另一个为 0.4,则第一个视图将占据屏幕的 60% 和其他 40%。这将阻止您将其他视图推离屏幕。

我修改了你的 XML 来说明它的用途

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <GridView
        android:id="@+id/propGridView"
        android:layout_width="0dip"
        android:layout_weight=".6"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:numColumns="5"
        android:paddingTop="20dp" >
    </GridView>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dip"
        android:layout_weight=".4"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/propertyThumbnail"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:layout_weight="0.10"
            android:paddingTop="20dp"
            android:src="@drawable/icon" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="5dp"
            android:text="Title"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Description"
            android:textSize="10dp" />
    </LinearLayout>

</LinearLayout>

我删除了您拥有的 FrameLayout,因为它基本上没用

【讨论】:

  • 我刚刚编辑了我的答案并从您的 XML 中删除了 FrameLayout。嵌套视图并没有做任何事情,而且不好(如果可以避免的话)。不过,结果看起来是一样的。
【解决方案2】:

这应该可以解决问题:

android:layout_width="MATCH_PARENT"
android:layout_height="MATCH_PARENT"

重新检查您的完整布局以查看将布局参数设置为“WRAP_CONTENT”的父视图。这些会搞砸你的布局。

【讨论】:

  • 这适用于所有视图还是仅适用于最顶部的视图?请看一下我上面贴的xml。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
相关资源
最近更新 更多