2017-08-09 6 views
0

私はLinearLayout内にTableLayoutを持っています。これは4x4グリッドです。私は各セルを正方形にして、画面サイズ(またはTableLayoutに割り当てられているサイズ)に基づいてサイズを拡大します。私はいつも画面に表示されている12個の四角形すべてが欲しい。基本的には、各ImageViewのlayout_heightlayout_wightの値を親の値の1/4にします。今、私はちょうど100dpに設定しました。 layout_weightの値で何かする必要がありますか?テーブルレイアウトの要素のサイズを画面のサイズに合わせて変更するにはどうすればよいですか?

編集:はい、layout_weight="1"をすべて私のImageViewオブジェクトに使用すると、幅をすべて利用可能なスペースの1/4にすることができましたが、今は高さが0になりました。高さをどのようにしてlayout_weightの関数にすることができますか?

これは私のXMLです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
    tools:context="com.package.name.MainActivity"> 

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/tableLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <!-- 1st row --> 
     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dip" > 

      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorAccent"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorPrimary"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorAccent"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorPrimary"/> 

     </TableRow> 

     <!-- 2nd row --> 
     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dip" > 

      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorPrimary"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorAccent"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorPrimary"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorAccent"/> 

     </TableRow> 


     <!-- 3rd row --> 
     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dip" > 

      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorAccent"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorPrimary"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorAccent"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorPrimary"/> 

     </TableRow> 

     <!-- 4th row --> 
     <TableRow 
      android:id="@+id/tableRow4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dip" > 

      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorPrimary"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorAccent"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorPrimary"/> 
      <ImageView 
       android:layout_height="100dp" 
       android:layout_width="100dp" 
       android:src="@color/colorAccent"/> 

     </TableRow> 

    </TableLayout> 


</LinearLayout> 

それは完全には表示されない右端の列に、次のようになります。私は100dpにそれらすべてが設定されているので、これは実現が、ちょうど私が現在よどこのアイデアを与えるために:あなたができる1android:stretchColumns="*"

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:stretchColumns="*"> 
</TableLayout> 
  • を使用することができます enter image description here

  • +0

    あなたは、画像ビューを使用しています。画像ビューに配置する画像がありますか、または正方形に色を入れるために使用していますか? – Cheticamp

    +0

    私は最終的にイメージを持っています、色は今のところプレースホルダです。私はちょっとこのアイディアを廃止し、代わりに 'GridLayout'を使いました。 – intA

    +1

    画像に1x1の比率がある場合、ImageViewsでadjustViewBounds = "true"と指定すると、セルは正方形になります。 – Cheticamp

    答えて

    0
    1. LinearLayoutを使用してください。

      <?xml version="1.0" encoding="utf-8"?> 
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:orientation="vertical"> 
      
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
             android:id="@+id/tableLayout1" 
             android:layout_width="match_parent" 
             android:layout_height="wrap_content"> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorAccent"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorPrimary"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorAccent"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorPrimary"/> 
      
      </LinearLayout> 
      
      <!-- 2nd row --> 
      <LinearLayout 
          android:id="@+id/tableRow2" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
      
           android:src="@color/colorPrimary"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorAccent"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorPrimary"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorAccent"/> 
      
      </LinearLayout> 
      
      
      <!-- 3rd row --> 
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
             android:id="@+id/tableLayout3" 
             android:layout_width="match_parent" 
             android:layout_height="wrap_content"> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorAccent"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorPrimary"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorAccent"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorPrimary"/> 
      
      </LinearLayout> 
      
      <!-- 4th row --> 
      <LinearLayout 
          android:id="@+id/tableRow4" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
      
           android:src="@color/colorPrimary"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorAccent"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorPrimary"/> 
      
          <ImageView 
           android:layout_width="100dp" 
           android:layout_height="100dp" 
           android:layout_weight="1" 
           android:src="@color/colorAccent"/> 
      </LinearLayout> 
      </LinearLayout> 
      
    関連する問題