Java 布局,Android-WeatherApp
我是android开发的新手,我正在努力制作一个需要尽快完成的应用程序。如果这对你来说不重要,我的问题很简单,“我在布局中放置的所有内容都是重叠和重复的”,因为功能,但我不知道如何避免它。Java 布局,Android-WeatherApp,java,android,xml,openweathermap,Java,Android,Xml,Openweathermap,我是android开发的新手,我正在努力制作一个需要尽快完成的应用程序。如果这对你来说不重要,我的问题很简单,“我在布局中放置的所有内容都是重叠和重复的”,因为功能,但我不知道如何避免它。改变话题至少对专家来说,这些是很容易的。 结果应该是有一个用于搜索城市的编辑文本和用于选择城市的选择按钮。如果我忘了加什么,请告诉我 Activity_Main.xml forecast_item_layout.xml 主要活动 公共类MainActivity扩展AppCompative实现Forec
改变话题至少对专家来说,这些是很容易的。
结果应该是有一个用于搜索城市的编辑文本和用于选择城市的选择按钮。如果我忘了加什么,请告诉我 Activity_Main.xml
forecast_item_layout.xml
主要活动
公共类MainActivity扩展AppCompative实现ForecasAdapter.OnForecasticItemClickListener{
私有静态最终内部请求位置权限=100;
私人场所经理场所经理;
私人气象服务;
私人回收站;
私人预报员预报员预报员;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupRecyclerView();
service=ApiService.getService();
getTestWeather();
locationManager=(locationManager)getSystemService(位置服务);
requestLocation();
}
私有void requestLocation(){
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
ActivityCompat.requestPermissions(
这
新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION,Manifest.permission.ACCESS\u rough\u LOCATION},
请求位置权限
);
回来
}
locationManager.RequestLocationUpdate(
LocationManager.GPS\u提供程序,
0,
0,
新位置侦听器(){
@凌驾
已更改位置上的公共无效(位置){
Log.d(“接收到位置”,Location.getLatitude()+”);
getCurrentWeatherByCoords(location.getLatitude(),location.getLatitude());
locationManager.RemoveUpdate(此);
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
Log.d(“位置状态”,“位置+状态”);
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
}
);
}
私人空间getTestWeather(){
service.getWeather(“Zagreb”,Config.WEATHER\u API\u KEY).enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.d(“天气响应”,response.body().getMain().getTemp().toString());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
}
});
service.getForecast(“zagreb”,Config.WEATHER\u API\u KEY).enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.d(“预测响应”,response.body();
setForecastItems(response.body().getForecastItem());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
}
});
}
专用void getCurrentWeatherByCoords(双lat,双lon){
getWeatherByCoords(lat、lon、Config.WEATHER\u API\u KEY).enqueue(新回调()){
@凌驾
公共void onResponse(调用、响应){
Toast.makeText(MainActivity.this,“当前位置”+response.body().getMain().getTemp()+”,Toast.LENGTH\u SHORT.show();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}
私有void setupRecyclerView(){
forecastRV=(RecyclerView)findViewById(R.id.forecastRV);
forecastRV.setLayoutManager(新直线布局经理(本));
ForecasAdapter=新ForecasAdapter();
前置适配器(前置适配器);
setListener(this);
}
@凌驾
ForecastitemClick上的公共无效(ForecastItem ForecastItem){
Toast.makeText(this,forecastItem.getMain().get湿度()+“”,Toast.LENGTH_SHORT).show();
}
}
如果您不想将按钮和EditText
放在forecast\u item\u layout.xml
中,为什么要将它们放在那里
将Activity\u 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>
<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>
保存recyclerview外观的视图的布局是什么like@tyczj我刚刚添加了activity_main.xml,其余代码与API和改装相关。一个否定,谢谢!!!你能至少解释一下为什么吗???我在版面中放置的所有内容都是重叠和重复的问题到底是什么?@forpas问题是当我添加这些编辑文本和按钮时,所有的设计都会损坏,正如你在屏幕截图上看到的,这些编辑文本和按钮会随着温度的变化而重复
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();
}
}
<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>
<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>