계산기 용 점토를 만들고자합니다. 이것은 내 MainActivity입니다. 버튼의 너비를 정의하기 위해 가중치를 사용하고 있습니다. 에뮬레이터에서 실행할 때 버튼이 전혀 표시되지 않습니다. 이게 잘 작동하는지 궁금 해서요 (그래서 실수, XML 또는 드로어 블을 검색 할 곳을 알기 위해서)?Android : 버튼 너비와 동일한 높이 설정 (테이블 레이아웃 내부)
@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));
}
}
는 편집 :
<?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 : roundedbutton 내 버튼을 5 tablerows
편집 2의 aparted 된 테이블 레이아웃 내부에 배치됩니다 : 이것은 내 main_activity.xml입니다 :
<?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" />
roundedarea :
<?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>
dimens :
<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">*-/^</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>
테이블 레이아웃에서 strechColumns를 시도 했습니까? –
@ cricket_007 아니, 필요한가요? – JAAAY
레이아웃 파일을 게시하십시오. – Vucko