2016-07-20 11 views
0

私の電卓のクレイアウトをしようとしています。これは私のMainActivityです。私は、ボタンの幅を定義するためにウェイトを使用しています。私はエミュレータ上でそれを実行すると、全くボタンが表示されません。私はこれがうまくいくかどうか疑問に思っています(間違い、xmlまたはdrawableをどこで検索するかを知るために)?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)); 
    } 
} 

EDIT:これは私の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> 

EDIT 3: roundedbutton:私のボタンは5 tablerows

EDIT 2のapartedされるテーブルレイアウトの内側に配置されています

<?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> 
+0

テーブルレイアウトで柱を伸ばしてみましたか? –

+0

@ cricket_007いいえ、必要ですか? – JAAAY

+1

レイアウトファイルを投稿してください。 – Vucko

答えて

0

ちょうどあなたのxmlの変化行います

変更

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

を3210

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

weightsumを割り当てるレイアウトの幅を指定する必要があります。

0

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

私のボタンはまだ画面に表示されません。私のテキストビューはそうです。 – JAAAY

関連する問題