我是android开发的新手,如果这对您来说不重要,我仍在努力开发一款仍需要尽快完成的应用程序,我的问题很简单,“我将所有内容放入布局中由于功能的缘故,它会覆盖并重复“”,但我不知道如何避免。
至少更改主题对于专家而言是容易的事情。
结果应该是具有用于搜索城市的EditText和用于选择城市的选择按钮。如果我忘记添加任何东西,请告诉我。
<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"
tools:context="com.example.danie.weather.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/forecastRV"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="18dp"
android:gravity="center_vertical"
android:background="?attr/selectableItemBackground">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/searchCity"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/sendBtn"
android:text="Select"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/forecastIV"
tools:src="@mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
<TextView
android:id="@+id/descriptionTV"
android:textSize="20sp"
android:layout_marginLeft="20dp"
tools:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public class MainActivity extends AppCompatActivity implements ForecastAdapter.OnForecastItemClickListener {
private static final int REQ_LOCATION_PERMISSION = 100;
private LocationManager locationManager;
private WeatherService service;
private RecyclerView forecastRV;
private ForecastAdapter forecastAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupRecyclerView();
service = ApiService.getService();
getTestWeather();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
requestLocation();
}
private void requestLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
REQ_LOCATION_PERMISSION
);
return;
}
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("Location received", location.getLatitude() + "");
getCurrentWeatherByCoords(location.getLatitude(), location.getLongitude());
locationManager.removeUpdates(this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Location status", "" + status);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
);
}
private void getTestWeather() {
service.getWeather("Zagreb", Config.WEATHER_API_KEY).enqueue(new Callback<CurrentWeather>() {
@Override
public void onResponse(Call<CurrentWeather> call, Response<CurrentWeather> response) {
Log.d("Weather response", response.body().getMain().getTemp().toString());
}
@Override
public void onFailure(Call<CurrentWeather> call, Throwable t) {
t.printStackTrace();
}
});
service.getForecast("zagreb", Config.WEATHER_API_KEY).enqueue(new Callback<WeatherData>() {
@Override
public void onResponse(Call<WeatherData> call, Response<WeatherData> response) {
Log.d("Forecast response", response.body().getForecastItem().toString());
forecastAdapter.setForecastItems(response.body().getForecastItem());
}
@Override
public void onFailure(Call<WeatherData> call, Throwable t) {
t.printStackTrace();
}
});
}
private void getCurrentWeatherByCoords(double lat, double lon) {
service.getWeatherByCoords(lat, lon, Config.WEATHER_API_KEY).enqueue(new Callback<CurrentWeather>() {
@Override
public void onResponse(Call<CurrentWeather> call, Response<CurrentWeather> response) {
Toast.makeText(MainActivity.this, "Current location " + response.body().getMain().getTemp() + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<CurrentWeather> call, Throwable t) {
}
});
}
private void setupRecyclerView() {
forecastRV = (RecyclerView) findViewById(R.id.forecastRV);
forecastRV.setLayoutManager(new LinearLayoutManager(this));
forecastAdapter = new ForecastAdapter();
forecastRV.setAdapter(forecastAdapter);
forecastAdapter.setListener(this);
}
@Override
public void onForecastItemClick(ForecastItem forecastItem) {
Toast.makeText(this, forecastItem.getMain().getHumidity() + "", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
如果不希望将Button
和EditText
放在forecast_item_layout.xml
内,为什么?
将Activity_Main.xml
更改为此:
<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"
tools:context="com.example.danie.weather.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/forecastRV"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:id="@+id/searchCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/sendBtn"/>
<Button
android:id="@+id/sendBtn"
android:text="Select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
和forecast_item_layout.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="18dp">
<ImageView
android:id="@+id/forecastIV"
android:layout_width="40dp"
android:layout_height="40dp"
tools:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/descriptionTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
tools:text="Text" />
</LinearLayout>