Android:调整图像按钮和TextViews的大小



我试图添加3个文本视图和ImageButtons到我的android布局。我一直在尝试调整它们的大小,以便所有6个组件都适合屏幕。

我相信这是一个非常简单的问题,但我不能解决它,这是非常令人沮丧的。

我该怎么做?

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tvFirstTabMaths"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Maths"
        android:textSize="15dp" />
    <ImageButton
        android:id="@+id/ibFirstTabMathsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/mathsicon" />
    <TextView
        android:id="@+id/tvFirstTabMemory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Memory"
        android:textSize="15dp" />

    <ImageButton
        android:id="@+id/ibFirstTabMemoryButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/memoryicon" />

     <TextView
        android:id="@+id/tvFirstTabStroop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stroop"
        android:textSize="15dp" />
    <ImageButton
        android:id="@+id/ibFirstTabStroopButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/stroopicon" />

</LinearLayout>

试着这样做:

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent" android:orientation="vertical">
        <TextView android:id="@+id/tvFirstTabMaths"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="Maths" android:textSize="15dp" android:layout_weight="1" />
        <ImageButton android:id="@+id/ibFirstTabMathsButton"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:src="@drawable/mathsicon" android:layout_weight=".5" />
        <TextView android:id="@+id/tvFirstTabMemory"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text=" Memory" android:textSize="15dp" android:layout_weight="1" />

        <ImageButton android:id="@+id/ibFirstTabMemoryButton"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:src="@drawable/memoryicon" android:layout_weight=".5" />

        <TextView android:id="@+id/tvFirstTabStroop"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="Stroop" android:textSize="15dp" android:layout_weight="1" />
        <ImageButton android:id="@+id/ibFirstTabStroopButton"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:src="@drawable/stroopicon" android:layout_weight=".5" />

    </LinearLayout>
</ScrollView>

如果你想让它们填满屏幕,你需要使用layout_weight属性。

将所有android:layout_height="wrap_content"改为android:layout_height="0dp",并添加android:layout_weight="1"


重量使视图共享可用空间。

权重可以是浮点数,也可以是任何你想要的值。这就像一个比率,如果你有一个在1,另一个在2,第二个会是第一个的两倍大。

你也可以在LinearLayout上使用weight_sum="10",然后如果你给你的子视图的权重小于10,就会有空白的空间。例如,你会得到6/10的屏幕与你的按钮和4/10空。

你也可以把没有权重的视图放在一个有权重的视图的容器中。第一个视图将占用它的空间,剩余的空间将与其他视图共享。

如果你正在使用线性布局,你应该设置android:layout_weight到儿童。这个属性根据一个视图在屏幕上应该占用多少空间来给它分配一个"重要性"值。较大的权重值允许它展开以填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组中任何剩余的空间将按照其声明的权重的比例分配给子视图

如果你想让所有6个视图水平适配,你应该尝试以编程方式获取屏幕大小,然后设置视图的宽度/参数。

view.setWidth(width/6);

最新更新