View not showing in a relative layout in Android [duplicate]

自古美人都是妖i 提交于 2019-12-13 08:18:40

问题


I posted this already but i got new information that may help you to help me... I have a problem with this layout and i cannot find out what is wrong. As you can see, there is a chronometer and a table inside a relative layout but the chronometer does not show, not even a blank space.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Chronometer
    android:id="@+id/chrono"
    android:textColor="#4169E1"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Chronometer"
/>
<TableLayout 
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
</TableLayout>
</RelativeLayout>

So i tried changing the order of the chronometer and tablelayout. I tried also changing the layout_width and layout_height properties, but i got nothing. So i went to my java class and i found out that the issue might be here:

 private final void createGameBoard(short gridSize) {
  DisplayMetrics metrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(metrics);

  TableLayout tableLayout;
  tableLayout = (TableLayout) findViewById(R.id.parentLayout);    
  tableLayout.removeAllViews();

  board = GameBoard.createGameBoard(this, 
        bitmap, 
        tableLayout,
        (int) (metrics.widthPixels * metrics.density),
        (int) ((metrics.heightPixels * metrics.density)-h1),
        gridSize);
}

where GameBoard.createGameBoard is:

public static GameBoard createGameBoard(Context context, 
                             Bitmap bitmap, 
                             TableLayout parentLayout,
                             int width,
                             int height,
                             short gridSize) {

  board = new GameBoard(context, 
                   bitmap, 
                   parentLayout, 
                   width, 
                   height, 
                   gridSize);

  return board;
}

So i guess that the problem displaying the chronometer comes from the creation of the gameboard, because of the metrics taken from the current window, which is used to set the height of the gameboard. any suggestions?


回答1:


Your xml code:

<Chronometer
    android:id="@+id/chrono"
    android:textColor="#4169E1"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Chronometer"
/>
<TableLayout 
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

This will force TableLayout to be shown in full screen and so, Chronometer is being hidden.

Try changing like one of followings:

1.)

<Chronometer
    android:id="@+id/chrono"
    android:textColor="#4169E1"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Chronometer"
/>
<TableLayout 
    android:id="@+id/parentLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
>

Which will wrap content of table, so Chronometer will get space.

2.)

<Chronometer
    android:id="@+id/chrono"
    android:textColor="#4169E1"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Chronometer"
/>
<TableLayout 
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/chrono"
>

This will force Chronometer to take full space below table layout after table layout is shown.

Hope this helps.



来源:https://stackoverflow.com/questions/24201301/view-not-showing-in-a-relative-layout-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!