2017-05-15 14 views
-1

enter image description hereどのように画面サイズに応じて動的にボタンのサイズを変更できますか?

私はテーブルレイアウトでボタンを持っています。私はここで

TableLayout tblHotels=(TableLayout) findViewById(R.id.tblLayout); 
     if(hotels != null){ 
      int index=0; 
      for (int i=0; i<(hotels.length/2); i++) { 
       TableRow tblRow=new TableRow(getApplicationContext()); 
       tblHotels.addView(tblRow); 
       tblRow.setLayoutParams(new TableLayout.LayoutParams(
         TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT)); 
       for (int j=0;j<2; j++) 
       { 
        tblRow.addView(hotels[index]); 
        hotels[index].setLayoutParams(new TableRow.LayoutParams(
          TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT, 1.0F)); 
        hotels[index].setOnClickListener(this); 
        index++; 
       } 
      } 
     } 

テーブルレイアウトで動的にボタンを置くしかし、私が同じ幅とちょっと大きめの高さで私のボタンをしたい画面のように表示されます。どうすればいいですか?私のxmlfileはこれのようなものです。

`<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.arzucaki.sherwoodhotels.Hotels" 
    android:weightSum="1" 
    > 


    <TableLayout 
     android:id="@+id/tblLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:alpha="0.6" 
     android:gravity="bottom" 
     android:orientation="vertical" 
     android:layout_alignParentTop="true"> 


    </TableLayout> 




</RelativeLayout>` 
+0

、その後、テーブルレイアウトが正しく表示されません。 list/recyclerview/gridviewレイアウトを使用する方が良いでしょう。 –

答えて

0

あなたは先に行くと、XMLファイルにテーブルの行を作成し、dimens.xmlファイル内のテーブルの行の高さを定義することができます。ここでは

は、サンプルの表現です:

<TableRow 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/table_row_height"> 

     <TextView 
      android:text="Time" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:layout_column="1" /> 

     <TextView 
      android:text="Time" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:layout_column="2" /> 
    </TableRow> 

追加各フォルダでサポートするすべての解像度のdimens.xmlファイル など。値は、値-w820dpなど ただ、これらのフォルダ内のdimensという名前の新しいファイルを作成し、このようなあなたの値を宣言します。この値は、値-w820dpの下に別のファイルに高くなります

<resources> 
<!-- Table Row height --> 
<dimen name="table_row_height">16dp</dimen> 

あなたの条件に基づいて、多分このよう:100(以上を意味する)のホテルがある場合

<resources> 
<!-- Table Row height --> 
<dimen name="table_row_height">24dp</dimen> 

+0

ok私は待ってみようとしています – arzucaki

+0

すべてのボタンは重さ1の画面を満たしています。画面サイズの異なるすべてのデバイスで同じように動く画面のパーセンテージのように、どのように高さを与えることができますか。 – arzucaki

+0

この場合、xmlファイルにテーブル行を定義し、mdpi、hdpi、xhdpi、xxhdpi、xxxhdpiなどのデバイスの解像度に応じてdimensファイルにサイズの次元を指定する必要があります さらに、match_parentをwrap_contentいくつかのパディングを提供します。この方法では、コントロールは完全な画面を占めず、パディングもいくつか提供します。しかし、デバイスの解像度に応じてサイズを変えなければならないという要件のためには、ファイルサイズを変更する必要があります。 – Saurabh7474

関連する問題