简体   繁体   English

Android布局,设置按钮具有相同的大小

[英]Android layout, setting buttons to have the same size

I'm am completely new to programming android apps in eclipse. 我是eclipse中编写android应用程序的新手。

With that said, I based myself off code on the internet and and came up with this xml file for my layout. 话虽如此,我基于互联网上的代码,并为我的布局提出了这个xml文件。 I can not get the four buttons to have the same size , and when I play around with the other parts, the progress bar and texts under it, they go bananas, displaying it off screen. 我不能让四个按钮具有相同的大小 ,当我玩其他部分,进度条和它下面的文本时,它们会变成香蕉,在屏幕上显示。

Could someone review the code and tell me what is gong on? 有人可以审查代码并告诉我什么是锣? I simply don't understand. 我根本就不明白。 Here is a picture. 这是一张照片。

在此输入图像描述

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/txt_currentDate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:gravity="center"
            android:text="Current Date"
            android:textSize="18dp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/progressBarRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <ProgressBar
            android:id="@+id/DailyCalorieProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:progress="80" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/textView1"
            android:gravity="center"
            android:text="Calorie Limit : "
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView2"
            android:gravity="center"
            android:text="2000  " >
        </TextView>

        <TextView
            android:id="@+id/textView3"
            android:gravity="center"
            android:text="Calories Left : "
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView4"
            android:gravity="center"
            android:text="552" >
        </TextView>
    </TableRow>

    <TextView
        android:id="@+id/textView5"
        android:gravity="center_horizontal"
        android:text="____________________" >
    </TextView>

    <TableLayout
        android:id="@+id/ButtonLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp"
        android:stretchColumns="0,1" >

        <TableRow
            android:id="@+id/buttonRow1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:paddingTop="10dp" >

        <Button
            android:id="@+id/btn_addFood"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="Add Food"/>

        <Button
            android:id="@+id/btn_addExercise"
            android:layout_width="130dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="Add Exercise"/>

        </TableRow>

        <TableRow
            android:id="@+id/buttonRow2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_recWeight"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"
            android:text="Record Weight" />

        <Button
            android:id="@+id/btn_sgstMeal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"
            android:text="Suggest Meal/Exercise" />

    </TableRow>
    </TableLayout>
</TableLayout>

Edits: 编辑:

It now almost works. 它现在几乎可以工作。

在此输入图像描述

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/txt_currentDate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:gravity="center"
            android:text="@string/CurrentDate"
            android:textSize="18dp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/progressBarRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <ProgressBar
            android:id="@+id/DailyCalorieProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:progress="80" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/textView1"
            android:gravity="center"
            android:text="@string/CalorieLimit"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView2"
            android:gravity="center"
            android:text="@string/CalorieLimitData" >
        </TextView>

        <TextView
            android:id="@+id/textView3"
            android:gravity="center"
            android:text="@string/CaloriesLeft"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView4"
            android:gravity="center"
            android:text="@string/CaloriesLeftData" >
        </TextView>
    </TableRow>

    <TextView
        android:id="@+id/textView5"
        android:gravity="center_horizontal"
        android:text="@string/Line" >
    </TextView>



        <TableRow
            android:id="@+id/buttonRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:paddingTop="10dp" >

        <Button
            android:id="@+id/btn_addFood"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="@string/AddFood"/>

        <Button
            android:id="@+id/btn_addExercise"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="@string/AddExercise"/>

        </TableRow>

        <TableRow
            android:id="@+id/buttonRow2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:paddingTop="10dp" >

        <Button
            android:id="@+id/btn_recWeight"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="@string/RecordWeight" />

        <Button
            android:id="@+id/btn_sgstMeal"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="@string/SuggestedMeal_Exercise" />

    </TableRow>
</TableLayout>

Your layout isn't really "efficient". 你的布局不是真正“高效”。 But anyway..to solve your problem you have to set your TableRow (the ones that contain your buttons) to 但无论如何..要解决您的问题,您必须将TableRow(包含您的按钮的那些)设置为

android:layout_width="fill_parent"
android:layout_height="wrap_content"

and all of your buttons to 和你的所有按钮

android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"

0dip does not make anything. 0dip什么都没做。 But because of the weight param android does the work for measuring the actual size anyway so you do not need to provide a "relevant" value 但由于权重param android无论如何都要测量实际大小,所以你不需要提供“相关”值

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

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