繁体   English   中英

Android tablelayout和按钮调整大小

[英]Android tablelayout and button resize

在这里你是我的问题:我想在android中做一个看起来像这个网格的布局:

在此输入图像描述

为此,我创建了这个布局:

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


    <TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_weight="0.3">

        <Button
            android:id="@+id/clientimain"
            android:layout_width="0dip"
            android:layout_weight="1" android:layout_height="fill_parent"/>



        <Button
            android:id="@+id/button1"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Button" />

     </TableRow>

     <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3" >

        <Button
            android:id="@+id/button2"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Button"/>

        <Button
            android:id="@+id/button6"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Button" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3" >

        <Button
            android:id="@+id/button3"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Button"
             />

        <Button
            android:id="@+id/button7"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Button"
             />

    </TableRow>

   </TableLayout>

   </RelativeLayout>

所以基本上它由表格布局中的6个按钮组成,正确设置了权重属性(或者我希望如此!)。 结果看起来很好,直到我尝试设置每个按钮的背景属性。 在这种情况下,按钮获取放入的图像的高度(宽度正常)。 例如,如果我设置大小为96px x 96px的背景图像,结果如下所示:

在此输入图像描述

有没有办法阻止此按钮“拉伸”并正确居中图像? 我试图更改每个表行的height属性,更改按钮最大高度属性,并使用图像按钮更改按钮类型(并使用想要的图标设置src属性)但我没有完成想要的结果。
我还重写了关于支持多个屏幕和每个基本布局的谷歌文档,但我没有找到任何解决方案。 提前感谢任何想要帮助我的人!

安德里亚

将表格行的布局高度设置为0dip,并将权重设置为1

<TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" >

这应该可以解决您的问题


更改

<Button
            android:id="@+id/clientimain"
            android:layout_width="0dip"
            android:layout_weight="1" android:layout_height="fill_parent"/>

<Button
            android:id="@+id/clientimain"
            android:layout_width="0dip"
            android:layout_weight="1" android:layout_height="fill_parent"/>

这对于我来说是值得一试的,值得一试,对我来说它是如何首先提供它,也许你可以尝试清理你的项目(项目>清洁项目),因为它应该是正确的,因为它是


你的代码应该是这样的:

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


    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <Button
                android:id="@+id/clientimain"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1" />

            <Button
                android:id="@+id/button1"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Button" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <Button
                android:id="@+id/button2"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Button" />

            <Button
                android:id="@+id/button6"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Button" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <Button
                android:id="@+id/button3"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Button" />

            <Button
                android:id="@+id/button7"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Button" />
        </TableRow>
    </TableLayout>

   </RelativeLayout>

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


    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher" />
        </TableRow>


        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <ImageButton
                android:id="@+id/imageButton3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher" />

            <ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher" />
        </TableRow>


        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <ImageButton
                android:id="@+id/imageButton5"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher" />

            <ImageButton
                android:id="@+id/imageButton6"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher" />
        </TableRow>

    </TableLayout>

   </RelativeLayout>

仍然没有改变任何东西,图像的大小有多大?

我知道这不是你想要的,但我建议使用GridView而不是TableRow布局。

我认为GridView应该更好用,因为它是通过视图回收和从AbsListView继承的东西实现的。 GridView更难以部署,因为您必须使用适配器,但如果您要加载大量繁重的视图(例如图像),它将有效地工作。 参考。

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:columnWidth="100dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</GridView>

当我应用背景图像时

android:background="@drawable/ic_launcher"

它工作得很好。

我认为这是因为图像的大小所以我应用了尺寸为160X160的其他图像,问题又回来了。 比我将相同的160X160图像应用于所有按钮,它们完美对齐。 这是我的代码(图像Gender.png是160X160像素)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/RelativeLayout1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:gravity="center"
 android:orientation="vertical" >

<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" 
    android:layout_weight="0.3">

    <Button
        android:id="@+id/clientimain"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@drawable/Gender" />

    <Button
        android:id="@+id/button1"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button" 
        android:background="@drawable/Gender"/>

 </TableRow>

 <TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.3" >

    <Button
        android:id="@+id/button2"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button"
        android:background="@drawable/Gender"/>

    <Button
        android:id="@+id/button6"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button" 
        android:background="@drawable/Gender"/>

</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.3" 
    >

    <Button
        android:id="@+id/button3"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button"
        android:background="@drawable/Gender" />

    <Button
        android:id="@+id/button7"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button"
         android:background="@drawable/Gender"/>

</TableRow>


    <TableRow
    android:id="@+id/tableRow4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.3" >

    <Button
        android:id="@+id/button77"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button"
        android:background="@drawable/Gender"
         />

    <Button
        android:id="@+id/button75"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button"
        android:background="@drawable/Gender"
         />

</TableRow>


</TableLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM