Android:设置高度等于按钮的宽度(在表格布局内)

时间:2016-07-20 17:14:06

标签: android android-layout android-button

我正试图为我的计算器制作一个粘土。这是我的MainActivity。我正在使用重量来定义按钮的宽度。当我在模拟器上运行它时,我看不到任何按钮。我想知道这是否可行(以便知道在哪里搜索错误,xml或drawable)?

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)? /index.php?slug=$1 [L]

编辑:我的按钮被放置在一个表格布局内,该布局由5个表格分开

编辑2:这是我的main_activity.xml:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_main);
    setButtonHeight();
}

protected void setButtonHeight () {
    ArrayList<Button> buttonAL= new ArrayList<Button>();

    int id=0;
    for (int i=0; i<=9; i++){
        id= getResources().getIdentifier("b"+i,"id", getPackageName());
        buttonAL.add((Button)findViewById(id));
    }

    buttonAL.add((Button)findViewById(R.id.bdiv));
    buttonAL.add((Button)findViewById(R.id.bmul));
    buttonAL.add((Button)findViewById(R.id.bminus));
    buttonAL.add((Button)findViewById(R.id.bplus));
    buttonAL.add((Button)findViewById(R.id.bequal));
    buttonAL.add((Button)findViewById(R.id.bc));
    buttonAL.add((Button)findViewById(R.id.bdot));
    buttonAL.add((Button)findViewById(R.id.bop));
    buttonAL.add((Button)findViewById(R.id.bcp));
    buttonAL.add((Button)findViewById(R.id.bpow));
    int size=0;

    for(Button button: buttonAL) {
        size = button.getLayoutParams().width;
        button.setLayoutParams(new TableRow.LayoutParams(size,size));
    }
}

编辑3: roundedbutton:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_bottom_margin"
android:paddingLeft="@dimen/activity_left_margin"
android:paddingRight="@dimen/activity_right_margin"
android:paddingTop="@dimen/activity_top_margin"
android:background="@color/activity_main_background"
tools:context="com.jj.calculator.MainActivity">


<EditText
    android:id="@+id/screen"
    android:text="@string/screen_text"
    android:background="@drawable/roundedarea"
    android:alpha="0.85"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="4dp"
    android:gravity="right|bottom"
    android:textColor="@color/light_grey"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:fontFamily="sans-serif-thin"
    android:lines="1"/>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/screen">

    <TableRow
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:weightSum="4">
        <Button
            android:id="@+id/b7"
            android:text="7"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/b8"
            android:text="8"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/b9"
            android:text="9"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/bdiv"
            android:text="/"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:weightSum="4">
        <Button
            android:id="@+id/b4"
            android:text="4"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/b5"
            android:text="5"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/b6"
            android:text="6"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/bmul"
            android:text="*"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>

    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:weightSum="4">
        <Button
            android:id="@+id/b1"
            android:text="1"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/b2"
            android:text="2"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/b3"
            android:text="3"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/bminus"
            android:text="-"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>

    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:weightSum="4">
        <Button
            android:id="@+id/b0"
            android:text="0"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/bop"
            android:text="("
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/bcp"
            android:text=")"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/bplus"
            android:text="+"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>

    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:weightSum="4">
        <Button
            android:id="@+id/bdot"
            android:text="."
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/bpow"
            android:text="^"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/bc"
            android:text="C"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>
        <Button
            android:id="@+id/bequal"
            android:text="="
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:background="@drawable/roundedbutton"
            android:textColor="@color/light_grey"
            android:textSize="30sp"
            android:fontFamily="sans-serif-thin"
            android:clickable="true"/>

    </TableRow>
</TableLayout>


</RelativeLayout>

roundedarea:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<gradient android:startColor="#000000" android:endColor="#3d3d3d"
    android:angle="270"/>

<stroke
    android:dashGap="2dp"
    android:width="4dp"
    android:color="@color/dark_grey"/>

<size
    android:width="60dp"
    android:height="60dp" />

颜色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="5dp">
<corners
    android:bottomRightRadius="8dp"
    android:bottomLeftRadius="8dp"
    android:topLeftRadius="8dp"
    android:topRightRadius="8dp"/>

<gradient android:startColor="#000000" android:endColor="#3d3d3d"
    android:angle="270"/>

<stroke
    android:dashGap="2dp"
    android:width="4dp"
    android:color="@color/dark_grey"/>
<size
    android:height="60dp"/>

梦诗:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="rounded_area">#5b5b5b</color>
<color name="activity_main_background">#5c5c5c</color>
<color name="light_grey">#949494</color>
<color name="dark_grey">#404040</color>
</resources>

的字符串:

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_top_margin">10dp</dimen>
<dimen name="activity_bottom_margin">10dp</dimen>
<dimen name="activity_left_margin">5dp</dimen>
<dimen name="activity_right_margin">5dp</dimen>
</resources>

androidManifest:

<resources>
<string name="app_name">Calculator</string>
<string name="screen_text">0123456*-/^</string>
</resources>

2 个答案:

答案 0 :(得分:0)

你可以这样做:

button.getLayoutParams().height = button.getLayoutParams().width;
button.requestLayout();

答案 1 :(得分:0)

只需在xml中进行更改:

更改

<TableRow
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:weightSum="4">

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="4">

您需要提及要为其指定权重的布局的宽度。