Android :设置高度等于按钮的宽度(在 table 布局内)
Android : Set height equal to width of a button (inside table layout)
我正在尝试为我的计算器制作一个粘土图。这是我的主要活动。我使用重量来定义按钮的宽度。当我在模拟器上 运行 它时,我根本看不到任何按钮。我想知道这是否可以正常工作(以便知道在哪里搜索错误,xml 或 drawable)?
@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));
}
}
编辑:我的按钮放置在一个 table 布局中,该布局有 5 table 行
编辑 2:这是我的 main_activity.xml :
<?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>
编辑 3:
圆形按钮:
<?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>
字符串:
<resources>
<string name="app_name">Calculator</string>
<string name="screen_text">0123456*-/^</string>
</resources>
androidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jj.calculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您可以简单地这样做:
button.getLayoutParams().height = button.getLayoutParams().width;
button.requestLayout();
只需更改您的 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">
您需要为分配权重和的布局提及宽度。
我正在尝试为我的计算器制作一个粘土图。这是我的主要活动。我使用重量来定义按钮的宽度。当我在模拟器上 运行 它时,我根本看不到任何按钮。我想知道这是否可以正常工作(以便知道在哪里搜索错误,xml 或 drawable)?
@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));
}
}
编辑:我的按钮放置在一个 table 布局中,该布局有 5 table 行
编辑 2:这是我的 main_activity.xml :
<?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>
编辑 3: 圆形按钮:
<?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>
字符串:
<resources>
<string name="app_name">Calculator</string>
<string name="screen_text">0123456*-/^</string>
</resources>
androidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jj.calculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您可以简单地这样做:
button.getLayoutParams().height = button.getLayoutParams().width;
button.requestLayout();
只需更改您的 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">
您需要为分配权重和的布局提及宽度。