2017-12-22 12 views
-1

私はいくつかの本に基づいて以下の設定をしています。しかし、アプリケーションがロードされてもプログレスバーが表示されません。起動時にプログレスバーが表示されない

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

private EditText et; 
private ImageView iv; 
private ProgressBar pb; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button b = (Button)findViewById(R.id.button); 
    et = (EditText) findViewById(R.id.edittext); 
    b.setOnClickListener(this); 

    iv = (ImageView)findViewById(R.id.image); 
    pb = (ProgressBar)findViewById(R.id.progressbar); 
    pb.setVisibility(View.VISIBLE); 

} 

@Override 
public void onClick(View v){ 
    switch(v.getId()){ 
     case R.id.button: 
      //String input = et.getText().toString(); 
      //Toast.makeText(MainActivity.this, input,Toast.LENGTH_SHORT).show(); //** 
      //pb.setVisibility(View.VISIBLE); 
      break; 
     default: 
      break; 
    } 
} 

そして、私のactivity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/text_view" 
    android:text="This is a text" 
    android:gravity="center" 
    android:textSize="24sp" 
    android:textColor="#00ff00" 
    /> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="button" 
    android:textAllCaps="false" 
    android:id="@+id/button" 
    /> 

<EditText 
    android:id="@+id/edittext" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Type sth here :)" 
    android:maxLines="2" 

    /> 

<ImageView 
    android:id="@+id/image" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/rose" 
    /> 

<ProgressBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/progressbar" 
    android:indeterminate="true" 
    android:layout_centerInParent="true" 
    /> 
</LinearLayout> 

私は、このリンクを参照:Android progressBar not showingをまだ私の場合には動作しません...バーが単にありません表示...

+0

strange。それはxmlファイルの最後に ""でなければなりません。上に追加しようとしましたが、何とか表示されません。 –

+0

ああ...なぜ下投票?私はこの質問が、同じ種類から区別するのに十分なユニークなので、この質問が役に立ったと思う... StackOverflowは、downvotingするときに理由を言うように、downvotingの人々に対して、より厳密な制御を行うべきである.. –

答えて

1

ルートレイアウトはLinearLayoutです。 android:layout_centerInParent Linearlayoutの子の属性ではありません。したがって、ProgressBarを中央に表示する場合は、親として相対レイアウトを使用する方がよいでしょう。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/text_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="This is a text" 
      android:textColor="#00ff00" 
      android:textSize="24sp" /> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="button" 
      android:textAllCaps="false" /> 

     <EditText 
      android:id="@+id/edittext" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="Type sth here :)" 
      android:maxLines="2" 

      /> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/rose" /> 

    </LinearLayout> 

    <ProgressBar 
     android:id="@+id/progressbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:indeterminate="true" /> 
</RelativeLayout> 
+0

それは動作する!まことにありがとうございます! 「android:layout_centerInParent」を使用していないときには機能しませんでしたがしかし、この解決策は機能し、これに固執することが賢明です。大変感謝@ADM、あなたは頭痛を解決しました! –

0

考えられる理由は、開発者のオプションでアニメーションをオフにすることです。 テストプロジェクトでレイアウトファイルを使用しています。プログレスバーが表示され、正常に動作します。 アニメーションをオフにして、ProgressBarが表示されないようにします。

+0

Samsungの携帯電話でアニメーション関連の機能をすべてオンにしましたが、うまくいきませんでした。それとも、アンドロイドスタジオでオンにしなければならないアニメーション機能がいくつかありますか? –

関連する問題