Фрагмент не появляется на хостинге - PullRequest
0 голосов
/ 29 ноября 2018

Я добавляю фрагмент к действию, как показано ниже в коде.но во время выполнения я обнаружил, что фрагмент никогда не показывается.Я рассмотрел фрагментный макет и его группу просмотра в основном макете.

, пожалуйста, дайте мне знать, почему он не отображается.

MainAct :

I am adding a fragment to an activity as shown below in the code. but at run time i found that the fragment is never shown.

Я рассмотрел фрагментный макет и его группу просмотра в основном макете.

, пожалуйста, дайте мне знать, почему он не отображается.

MainAct :

public class ActMain extends AppCompatActivity {

public static String TAG = ActMain.class.getSimpleName();
private FragmentButtons mFragButtons = null;
//private Fragment mFragmentButtons = null;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "@onCreate");

    this.initViews();
}
}

frag

public class FragmentButtons extends Fragment {

private final static String TAG = FragmentButtons.class.getSimpleName();
public interface iActionHandler {
    public void onButton00Clicked();
    public void onButton01Clicked();
    public void onButton02Clicked();
    public void onButton03Clicked();
}



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.d(TAG, "@onCreateView");
    View view = inflater.inflate(R.layout.frag_buttons, container, true);
    return view;
}
}

mainLayout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">

<fragment
    android:id="@+id/actMain_FragButtons"
    android:name="com.example.pc_amr.servicewithid_01.fragments.FragmentButtons"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<!--<FrameLayout
    android:id="@+id/actMain_FragContents00"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"/>-->


</LinearLayout>

fragLayout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="4">

    <Button
        android:id="@+id/btn00"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn01"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn02"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn03"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="4">

    <Button
        android:id="@+id/btn10"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn11"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn12"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn13"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="4">

    <Button
        android:id="@+id/btn20"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn21"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn22"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn23"
        android:layout_width="0dp"
        android:onClick="onButtonClicked"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

Ответы [ 2 ]

0 голосов
/ 29 ноября 2018

Я использовал этот способ, и он работал со мной

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View v=inflater.inflate(R.layout.fragment_1_layout, container, false);
return v;}
0 голосов
/ 29 ноября 2018

Причина, по которой он не отображается, заключается в том, что в вашем методе onCreate вы не сделали setContentView, и поэтому он пуст и не отображается.

Добавьте следующую строку:

setContentView(R.layout.mainLayout);

Это должно позволить показать фрагмент.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...